Author Archives: artandfact_admin

How to build a Nagios plugin using a simple example

A Nagios plugin can be built using practically any language. There are only a few requirements:

  1. A set of one or more inputs (parameters) which the Nagios server will send in order to give it essential information to operate (such as which host to monitor, thresholds, etc.)
  2. Text output of the program, showing the result of the check. The first line will be shown in the services preview of the host in the Nagios web interface. Clicking in to the service will show all of the output.
  3. An exit code returned by the plugin program to indicate the results of the check. That is, the current state of the plugin. This will be: “OK”, “Warning”, or “Critical”.

The text below is an example plugin written in Bash which scans a host using nmap and tests the result against a known MD5 hash of the expected output. If the status of any of the ports change, then the MD5 hash will change and return a ‘CRITICAL’ state.

The output of the nmap scan and the hash are printed out by the plugin in all cases and is available by clicking on the service in the web interface.

#!/bin/bash

NMAPOUTPUT=$(nmap -oG - $1 | grep "Ports")

NMAPOUTPUTHASH=$(echo $NMAPOUTPUT | md5sum | cut -d ' ' -f 1)

if [ $NMAPOUTPUTHASH == $2 ]; then
        echo "OK: Hash of nmap portscan consistent."
        STATUS=0
else
        echo "CRITICAL: Hash of nmap portscan inconsistent."
        STATUS=2
fi

echo $NMAPOUTPUT
echo $NMAPOUTPUTHASH

exit $STATUS

This plugin is run using two parameters: hostname and md5sum. In bash, $0 is the name of the program which is being run. $1 is the first parameter, here the hostname. $2 is the second parameter, here the md5sum.

Nagios will interpret the exit status of the plugin as the state of the plugin check. All plugins will return with an exit code of 0, 1, or 2. These correspond to: “OK”, “Warning”, and “Critical”, respectively.

Here is a useful guide on how to configure Nagios to use a new plugin you just wrote:

THE WEIGHT OF PAPER

“Just as Archimedes had found the volume of an irregular shape by measuring the volume of water it displaced, so Galileo discovered a practical solution to the problem of finding the area under a curve. In the absence of geometric and algebraic tools to calculate the area, he would plot his curve, then cut it out and weigh the paper. By comparing the weight with the weight of a piece of paper of known area, he could work out the area of his curve.”

Rooney, Anne. The Story of Mathematics. Arcturus, 2009. p146.

An interesting exchange with Chat GPT regarding predicting Bitcoin price

Q: Historically, what is the most reliable and accurate leading indicator of future Bitcoin price?

A: Historically, the most reliable and accurate leading indicator of future Bitcoin price has been the Google Trends search volume for the term “Bitcoin”. This is because the search volume is a good indicator of the level of public interest in Bitcoin, which is often a good predictor of future price movements.

Q: Is that a leading indicator?

A: Yes, Google Trends search volume for the term “Bitcoin” is a leading indicator of future Bitcoin price.

Q: Approximately what time period does it lead?

A: The Google Trends search volume for the term “Bitcoin” typically leads the Bitcoin price by a few weeks to a few months.

Q: Can you provide an r-value for the correlation between Google Trends search volume for term “Bitcoin” and Bitcoin price? The correlation between Google Trends search volume for the term “Bitcoin” and Bitcoin price is typically around 0.7, indicating a strong positive correlation.

Sorting a list of CIDR IP addresses

You may have a list of IP addresses which follow CIDR format, i.e. 111.12.23.45/16 and which you wish to sort. Here is an algorithm to do so in Microsoft Excel:

Step 1. Paste all IP addresses in to a single column A in your Excel sheet.

Step 2. Remove the CIDR part of the IP address in column A by applying the following formula to the first row in Column B:

=LEFT(A1,FIND("/",A1)-1)

This will turn an IP address, such as 111.12.23.45/16 in to 111.12.23.45.

Step 3. Once you get a valid IP address in Column B, double click on the bottom-right corner of the cell to propagate for formula to the rest of the rows in Column B.

Step 4. Select the entire Column B and then paste again using Paste Special… Values. This will remove all the formulas and retain only the IP values.

Step 5. Apply the following formula in Column C in order to make sure each octet of your IP address has three digits:

=TEXT(LEFT(B1,FIND(".",B1,1)-1),"000") & "." & TEXT(MID(B1,FIND( ".",B1,1)+1,FIND(".",B1,FIND(".",B1,1)+1)-FIND(".",B1,1)-1),"000") & "." & TEXT(MID(B1,FIND(".",B1,FIND(".",B1,1)+1)+1,FIND(".",B1, FIND(".",B1,FIND(".",B1,1)+1)+1)-FIND(".",B1,FIND(".",B1,1)+1)-1), "000") & "." & TEXT(RIGHT(B1,LEN(B1)-FIND(".",B1,FIND(".",B1,FIND( ".",B1,1)+1)+1)),"000")
[*] https://www.extendoffice.com/documents/excel/4946-excel-sort-ip-address.html

This will convert all your IPs (without CIDR notation) in to a set of numbers with the following format: XXX.XXX.XXX.XXX. That is to say, each octet will contain exactly three digits. In cases where an octet originally has only two digits, then a zero will be prepended. In the case where the octet originally has only one digit, then two zeros will be prepended.

For example, our original IP: 111.12.23.45/16 will now look like: 111.012.023.045

Step 5. Apply the same thing you did in Step 4. but to this new column C, so that only the values remain.

You should now have three columns in your Excel sheet. The first column is a list of your original IPs in CIDR format. The second is a list of the same IPs without the CIDR notation. The third is a list of the same IPs where each octet has precisely three digits.

Step 6. The final step is to sort the list by column 3: Select the whole sheet by clicking on the top-right corner of the sheet where the rows and columns meet. Go to Menu Item: ‘Data’… click on the ‘Sort’ icon, and select Column 3.

One caveat for using this algorithm:

This algorithm does not take into account the CIDR values when sorting.

‘Check WordPress Update’ plugin for Nagios — PHP 8 compatibility

If you’re using Nagios to monitor your WordPress installations, you may be using the ‘Check WordPress Update’ plugin by jinjie found here:

https://exchange.nagios.org/directory/Plugins/CMS-and-Blog-Software/Wordpress/Check-WordPress-Update/details

We recently updated our PHP version on some of our servers to version 8, and this broke the old version of the plugin. The new plugin has been updated and is compatible with PHP version 8.

The incompatibility was with the last line in the code:

echo $status . '#' . implode(';', $text);

The issue arises because of the order of the arguments for the implode function. The old signature, used for PHP versions up to 7.4 was:

implode(array $array, string $separator): string

whereas the new signature for PHP versions greater than 7.4 is:

implode(string $separator, array $array): string

If you’re on a machine which hosts multiple PHP versions and want to just use one version of the plugin, you can use this little modification to check the current PHP version:

//Test if we're using php version 8 or greater.  This fix is needed or a fatal error will occur.
if (version_compare(phpversion(), '8', '<')) {
    // php version is below 8
    echo $status . '#' . implode($text, ';');
} else {
    echo $status . '#' . implode(';', $text);
}

Windows Keyboard Shortcuts!

I just came across this Tweet by @Rainmaker1973, which is sharing a video by someone called lklogic on TikTok. I found it very useful. I’ve used most of these, but didn’t know about Control D to set to the Default Font Set or that you could align text via a shortcut with Control L and R.

You may want the text version, so I recreated it here:

  • Control A = Select All
  • Control B = Bold
  • Control C = Copy
  • Control D = Default Font Set
  • Control E = Center
  • Control F = Find
  • Control G = Go to
  • Control H = Replace
  • Control I = Italic
  • Control J = Justify
  • Control K = Hyperlink
  • Control L = Alight Text to Left
  • Control M = Hanging Indent
  • Control N = New Document
  • Control O = Open
  • Control P = Print
  • Control Q = Add Space after Paragraph
  • Control R = Align Text to Right
  • Control S = Save As
  • Control T = Left Indent
  • Control U = Underline
  • Control V = Paste
  • Control W = Close
  • Control X = Cut
  • Control Y = Repeat / Undo
  • Control Z = Undo