Code – James Batchelor https://james-batchelor.com Useful I.T & VoIP Ramblings Wed, 18 Oct 2023 10:31:51 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.5 https://james-batchelor.com/wp-content/uploads/2025/05/cropped-cropped-logo-jb-202505-32x32.png Code – James Batchelor https://james-batchelor.com 32 32 Manually install net-snmpd on Gentoo https://james-batchelor.com/index.php/2023/10/18/manually-install-net-snmpd-on-gentoo/ Wed, 18 Oct 2023 18:25:00 +0000 https://james-batchelor.com/?p=895 Continue reading "Manually install net-snmpd on Gentoo"]]> Recently we’ve moved from an aged Opsview instance to Zabbix for our system health monitoring, which in turn facilitated moving data collector agents from Nagios to snmp.

Many of our PBX’s were deployed from the vendors ISO and so run atop of Gentoo, and it has a couple of issues:

  1. We’ve been told not to “emerge” anything by the vendor, as the base OS on the image is not maintained.
  2. Portage (Gentoo’s package manager) has fallen out of date, meaning even if emerge is attempted, it’ll fail as all repository links are broke.

If faced with the same issue, this is how to install net-snmpd from source, add it a startup service and be able to monitor via snmp…

Installation

On the Gentoo box, move to your home directory

cd ~

Download the source .tar.gz file

wget https://sourceforge.net/projects/net-snmp/files/net-snmp/5.9.4/net-snmp-5.9.4.tar.gz

Visit http://www.net-snmp.org/download.html for the latest version. When I was downloading SourceForge’s SSL cert had expired, if this happens add –no-check-certificate after wget in the command above.

Untar the file to your home directory, and change into it

tar -xzvf net-snmp-5.9.4.tar.gz
cd net-snmp-5.9.4

Run the configuration script

./configure

This allows you to set snmp version, file installation locations and snmp details, with exception of snmp version these can be left as default (Hit enter at the prompt). snmp details will look like this if left default…

Compile and install snmpd

make
make install

The make process is quite CPU intensive, may want to do this during a quiet period for the server.

Some libraries are not where they need to be, namely:
libnetsnmpagent.so.40
libnetsnmpmibs.so.40
libnetsnmp.so.40

Find where they were installed

find / -name libnetsnmpagent.so.40

Pick the result that is not in your current working folder (the install setup) and link it to /usr/lib

ln -s /usr/local/lib64/libnetsnmpagent.so.40 /usr/lib/

Repeat this process with the other two files

ln -s /usr/local/lib64/libnetsnmpmibs.so.40 /usr/lib/
ln -s /usr/local/lib64/libnetsnmp.so.40 /usr/lib/

Now can test to see if snmpd can run

snmpd -v

Configuration

Create a configuration file, the “snmpconf” command can be used but I found it easier to create from scratch

nano /usr/local/share/snmp/snmpd.conf

For my needs, only this line is needed

rocommunity {community} {ip address}

Save and exit the editor.

Testing

If you’d like to test your configuration, run snmpd

snmpd

Test on the snmp target machine, for quick results snmpwalk can be run

snmpwalk -c {community} -v 2c {ip address}

When run, snmpd automatically switches to a background process. To stop the process ID needs to be found and killed

ps aux | grep snmpd

Here the process ID (PID) is 5716, use this with the kill command

kill -9 {PID}

Add Service

Create the new service file

nano /etc/init.d/snmpd

Paste the following

#!/sbin/openrc-run
 
depend() {
    after modules
}
 
start() {
    ebegin "Starting snmpd"
    start-stop-daemon --background --start --exec /usr/local/sbin/snmpd --pidfile /var/run/snmpd.pid \
    -- -p /var/run/snmpd.pid -c /usr/local/share/snmp/snmpd.conf
    eend $?
}
 
stop() {
    ebegin "Stopping snmpd"
    start-stop-daemon --stop --exec /usr/local/sbin/snmpd \
    --pidfile /var/run/snmpd.pid
    eend $?
}
restart() {
    ebegin "Restarting snmpd"
    start-stop-daemon --stop --exec /usr/local/sbin/snmpd
    start-stop-daemon --background --start --exec /usr/local/sbin/snmpd --pidfile /var/run/snmpd.pid \
    -- -p /var/run/snmpd.pid -c /usr/local/share/snmp/snmpd.conf
    eend $?
}

Save and exit the text editor, then make the file executable

chmod +x /etc/init.d/snmpd

Start the service, and check its running

/etc/init.d/snmpd start

/etc/init.d/snmpd status

Finally, to make snmpd start with the system, run

rc-update add snmpd default

References

Help Creating init.d file

https://big-elephants.com/2013-01/writing-your-own-init-scripts/
https://tecadmin.net/startup-shutdown-script-on-gentoo/

Experienced issue where the process-id in the PID file was not matching the PID of process. With help of below found flag for snmpd to specify PID file, and match that to init.d

https://forums.gentoo.org/viewtopic-t-886230-start-0.html

]]>
Automated Timelapse: Update https://james-batchelor.com/index.php/2017/12/31/automated-timelapse-update/ https://james-batchelor.com/index.php/2017/12/31/automated-timelapse-update/#comments Sun, 31 Dec 2017 23:29:44 +0000 http://james-batchelor.com/?p=555 Continue reading "Automated Timelapse: Update"]]> Its been a year since my time-lapse post, and since my server has been working away without external input capturing and archiving the view from from my window the entire time.

Something I wanted to capture is a time-lapse of of summer and winter from the same perspective, in order to see the difference between the two polar seasons in terms of sunlight.

Now with a year’s worth of capture and as a tribute to the reliability of the code, I thought to quickly splice together a June and December time lapse video:

 

 

Its interesting to see how the webcam when left at the default settings interprets light values over a sustained period of time. As even in the summer it will get as dark as the winter night at some point, I thought of this as a natural reset point for the cam’s light values.

Choosing a similar weather conditions at both ends of the daylight spectrum (23/06 and 19/12) the camera seemingly registers a different light strength.

Anyways it is still an interesting watch regardless of the date its being viewed at, hope you enjoy, and wishing you a happy new year!

 

]]>
https://james-batchelor.com/index.php/2017/12/31/automated-timelapse-update/feed/ 1
Update: Raspberry Pi Temperature Display https://james-batchelor.com/index.php/2016/10/30/update-raspberry-pi-temperature-display/ Sun, 30 Oct 2016 17:33:47 +0000 http://james-batchelor.com/?p=501 Continue reading "Update: Raspberry Pi Temperature Display"]]> With the cooler outdoor temperatures, a bug in my original code for the temperature display has cropped up:

IMG_6295

The DS18B20 returns temperatures with up to three decimal places, with the decimal point omitted, therefore a temperature of 10oC would be shown as 10000 by the sensor. To deal with this and for the display to show the temp to one decimal, I used this code:

        temperature1a = str (tempdata1[2:-3])

temperature1b = str (tempdata1[4:-2])

temperature1 = str ((temperature1a) + ‘.’ + temperature1b))

Here the value is essentially split in two, the number before the decimal and the number after, then the third line of code joins them back together with the decimal point in place. This is achieved by reading the value, then chopping off a number of characters using the [2:-3], this would chop off 2 characters at the start of the string, and 3 from the end.

The bug arises when the ambient temperature falls below 10oC as the sensor will output a 4 integer value as opposed to 5. This in turn will cause the cropping function to read incorrect values as its operation depends on a constant integer length.

As a result, the code will still function, but lower temperature values would be displayed without any decimalisation. Not happy with this I worked on another solution:

First Attempt

Referencing back to the original post on REUK, the code floated the text value into and integer, then a division of the value by 1000 gave the value a decimal point. Expanding on this I thought to keep the value as an integer to add the decimal, then convert to a string to allow the chopping off of the extra decimal places.

On testing however, I was getting inconsistent values, some values displayed with a decimal, others without.

I discovered that the DS18B20 would output a constant 5 or 6 figure value, however when floating to an integer, any trailing zeros after the first decimal place is removed. Therefore the snipping could again corrupt the output.

Result

Until I can find a better solution, it is better to keep it as simple as possible and display the temperature values intact, except the division by 1000 to add the decimal. Luckily the 20×4 LCD display is large enough to accommodate this, with only minor layout tweaks.

IMG_6303

For those interested, here is the amended code to read the sensor and output the value:

        tempfile1 = open(“/sys/bus/w1/devices/28-000007f34893/w1_slave”)

temptext1 = tempfile1.read()

tempfile1.close()

tempdata1 = temptext1.split(“\n”)[1].split(” “)[9]

temperature1 = float(tempdata1[2:])

temperature1 = temperature1 / 1000

]]>
Automated Time-lapse Solution https://james-batchelor.com/index.php/2016/09/03/automated-time-lapse-solution/ https://james-batchelor.com/index.php/2016/09/03/automated-time-lapse-solution/#comments Sat, 03 Sep 2016 20:26:08 +0000 http://james-batchelor.com/?p=439 Continue reading "Automated Time-lapse Solution"]]> A while ago I worked on a simple CCTV system for work, which involved using software to capture images every second then batch convert them to video every five minutes. It was crude but did the job.

I also dabbled in the past with time lapse videos, however this was a more manual process with images capturing to a folder, then personally loading them into Windows Movie Maker to create the video. With the tedium of creating the videos, the software I used for capturing (YAWCam) would hang after a few weeks constant running, not to mention without upkeep, the hundreds of thousands of image files populating the hard drive.

As a project it was time to combine the learnings from these and create an automated time lapse video creator, a program that would capture images, then create a 5-minute video that contains the days’ footage, and finish by deleting the temporary images to leave just the days video.

image104062

As with the CCTV project, a batch file will be used to create the process as what I need can be achieved by DOS commands plus it’s able to be called by Windows Task Scheduler. This time I will have the advantage of using an IP camera instead of USB, so negating the need for software to capture images, however the WGet utility is needed to download and save the images locally. Also the script will invoke FFMpeg to convert the images to video at the end of the day.

Setup

Getting started, place the WGet utility in the System32 folder so it can be called from dos without specifying a path. Then create this folder hierarchy on the root of C:\ drive:

Timelapse File Structure

Here’s the code for capture.bat, I placed this in the Data folder with ffmpeg.exe for neatness:

@echo off
::SET DATE TO A VARIABLE
set todaydate=%date:~-4%-%date:~3,2%-%date:~0,2%
::RESET COUNTER TO ZERO
set counter=100000

::CREATE TODAYS FOLDER AND NAVIGATE TO IT
echo Creating Folder…
c:
cd \cam\Data\temp\
md %todaydate%
cd %todaydate%
::LOOP START
echo Capturing Images…
:capture
::GET IMAGE FROM WEBCAM CGI
wget -q http://xxx.xxx.xxx.xxx/image/jpeg.cgi

::RENAME FILE USING COUNTER
ren jpeg.cgi image%counter%.jpg

::INCREMENT COUNTER BY ONE
set /a counter=counter+1

::SET TIME TO VARIABLE
set hour=%time:~0,2%
if “%hour:~0,1%” == ” ” set hour=0%hour:~1,1%
set min=%time:~3,2%
if “%min:~0,1%” == ” ” set min=0%min:~1,1%
set secs=%time:~6,2%
if “%secs:~0,1%” == ” ” set secs=0%secs:~1,1%
set thetime=%hour%%min%%secs%

::TIME BEFORE NEXT IMAGE
timeout /t 3 >nul

::CHECK ITS NOT END OF DAY
if %thetime% LSS 120600 (
goto capture
) else (
::START CONVERTING VIDEO
echo Processing Timelapse Video…
“C:\cam\Data\ffmpeg.exe” -loglevel quiet -i C:\cam\Data\temp\%todaydate%\image1%%05d.jpg -r 28 -c:v libx264 C:\cam\Video\%todaydate%.mp4
echo Deleting temp files…
::TIME DELAY TO ALLOW PROCESS TO CLOSE
timeout /t 10 >nul
::CHANGE DIRECTORY OUT OF FOLDER TO BE DELETED THEN DELETE TEMP FILE
cd \cam\Data\temp\
RD /S /Q “C:\cam\Data\temp\%todaydate%”
)
echo Process Complete

Next is to create a Scheduled Task in Windows to launch the program each day at midnight, this process varies by system but ensure that user privileges are high enough to write and execute on disk.

Timelapse Scheduled Task

I found Windows Server 2012 R2 had a lot more options over Server 2003, so I set the task to run when even when not logged on, to run hidden, and now the time can be specified to the second, so chose to start task at 00:00:03 to make sure the date is correct.

Notes

Hopefully the annotation will be helpful enough if you would like to apply your own tweaks, but here are some of notes about the issues I faced when creating the script:

  • Originally the images named with a timestamp, however FFMpeg requires images to be sequentially numbered to create the slideshow.
  • The counter was set at 000000, but on the first loop the arithmetic to increase the counter changed the value to 1, so lines were added to include the leading zeros
  • The leading zeros meant that the machine was reading the number as an octet, so on the 8th loop the counter reset to 000001, as a workaround a leading 1 was added to the counter.
  • Capturing images every 10 seconds equates to 8640 images a day, the counter it set to five decimals to ensure no looping of counter numbers.
  • In addition to storing the date as a variable to allow multiple calls, it allows the program to keep the same date after time moves into the next day. Since the video conversion starts at 23:59 each day its very likely the process will move into the next day when temp files are deleted.
  • Initially the time variables were inside the IF statement, but learned that the variable with an IF statement are created when the program is parsed, not run, therefore would not update.
  • However, the time needs to be in the loop to update before the IF statement is reached, the date is purposely kept out to it retains the value set at the start of the script.
  • While statements don’t really exist in batch files, so goto and If commands were used to create a crude while loop.

I dare not say that the code is perfect, or if the choice of programming language is suitable, the aspiration would be to create a self contained executable with a configuration file to specify target folders, change finish time and time between image captures. But for now it’s doing the job just fine.

Update

Since creating the program I’ve reused parts of the code to create a preview program that compiles the images taken so far that day into a video, without deleting any files. Here’s the code, again, save as a bat file and double click to run immediately.

@echo off

echo Establishing todays date…
::FIND TODAYS DATE TO ESTABLISH FOLDER NAME
set todaydate=%date:~-4%-%date:~3,2%-%date:~0,2%

::FIND TIME TO CREATE FILENAME VARIABLE
set hour=%time:~0,2%
if “%hour:~0,1%” == ” ” set hour=0%hour:~1,1%
set min=%time:~3,2%
if “%min:~0,1%” == ” ” set min=0%min:~1,1%
set secs=%time:~6,2%
if “%secs:~0,1%” == ” ” set secs=0%secs:~1,1%
set thetime=%hour%%min%%secs%

echo Locating temp files…
::NAVIGATE TO IMAGE FOLDER
C:
cd \cam\Data\temp\
cd %todaydate%

echo Creating preview video
::CREATE VIDEO AND ADD PREVIEW IN FILENAME TO AVOID DUPLICATE FILENAMES
“C:\cam\Data\ffmpeg.exe” -loglevel quiet -i C:\cam\Data\temp\%todaydate%\image1%%05d.jpg -r 28 -c:v libx264 V:\Timelapse\Video\preview-%todaydate%-%thetime%.mp4

echo Complete

]]>
https://james-batchelor.com/index.php/2016/09/03/automated-time-lapse-solution/feed/ 1