Posts Tagged ‘shell script’

a nagios check to remind me the SSL certificate expiration

Monday, March 3rd, 2014

I wrote a quite unuseful check for nagios to remind me to renew my SSL certificate. This is the definition in commands.cfg file

define command{
        command_name check_ssl_expiration
        command_line /usr/lib/nagios/plugins/check_ssl_expiration.sh $ARG1$ $ARG2$ $ARG3$ $ARG4$
}

and this the check_ssl_expiration.sh script

#!/bin/bash
# input parameters
MYSRV=$1
MYPORT=$2
DAYWARN=$3
DAYCRIT=$4
# return values
RET_OK=”0″
RET_WARN=”1″
RET_CRIT=”2″
RET_UNKN=”3″
TEMPFILE=/tmp/.$$certtest.pem

# check data input
checkdata () {
        VAL=`echo $2 | wc | awk ‘{print $2}’`
        if [ $VAL -eq 0 ]; then
                echo $1 is not set
                exit $RET_UNKN
        fi
}

checkdata “HTTPS server name” $MYSRV
checkdata “HTTPS PORT” $MYPORT
checkdata “warning threshold” $DAYWARN
checkdata “critical error threshold” $DAYCRIT

echo | openssl s_client -connect $MYSRV:$MYPORT  2> /dev/null | sed -ne ‘/-BEGIN CERT/,/-END CERT/p’ > $TEMPFILE 2>/dev/null
EXPDATE=`openssl x509 -noout -in $TEMPFILE -dates|grep notAfter|sed -e “s/.*notAfter=//”`
rm $TEMPFILE

EXPSEC=`date “+%s” –date=”$EXPDATE”`
NOWSEC=`date “+%s”`
DAYLEFT=`expr \( $EXPSEC – $NOWSEC \) / 86400`

# $DAYLEFT days left to SSL certificate expiration

if [ $DAYLEFT -le $DAYCRIT ]; then
        echo “ERROR – $DAYLEFT days left to SSL certificate expiration for $MYSRV:$MYPORT”
        exit $RET_CRIT
fi

if [ $DAYLEFT -le $DAYWARN ]; then
        echo “WARNING – $DAYLEFT days left to SSL certificate expiration for $MYSRV:$MYPORT”
        exit $RET_WARN
fi

echo “$DAYLEFT days left to SSL certificate expiration for $MYSRV:$MYPORT”
exit $RET_OK

Off course I scheduled this check once a day.

monitoring a remote webserver with freeshell.de

Wednesday, February 16th, 2011

During this last week a webserver of mine appears to be very slow. This webserver is made by a CMS, so any page is load after making some SQL query to a DB.
Since a wget of a static jpg image has a normal speed, while is not fast as usual to load any page, I convinced myself to monitor the database performances.
I made a simple monitoring by 3 steps.
Step 1: I wrote a php simple page for my webserver to make a db connection, executing a couple of queries and telling me the needed time to get te response. Calling my http://www.mywebsite.org/test.php I’m able to read the result. My php script is

<?php
function diff_microtime($mt_old,$mt_new) {
list($old_usec, $old_sec) = explode(‘ ‘,$mt_old);
list($new_usec, $new_sec) = explode(‘ ‘,$mt_new);
$old_mt = ((float)$old_usec + (float)$old_sec);
$new_mt = ((float)$new_usec + (float)$new_sec);
return $new_mt – $old_mt;
}

$ti=microtime();
echo “STARTING SQL EXECUTION: “. $ti .”<br>”;
$dbhost = ‘localhost’;
$dbuser = ‘mydbusername’;
$dbpass = ‘supersecretpassword’;
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die (‘Error connecting to mysql’);

$dbname = ‘mydatabase’;
mysql_select_db($dbname);

echo ‘TABLE STRUCTURE FOR <i>cms_module_visitorstats</i>:<br>’;
$res = mysql_query(‘DESCRIBE cms_module_visitorstats’);
while($row = mysql_fetch_array($res)) {
echo “{$row[‘Field’]} – {$row[‘Type’]} <br>\n”;
}
$res = mysql_query(‘SELECT count(*) from cms_module_visitorstats;’);
while($row = mysql_fetch_array($res)) {
echo “<br>{$row[0]} Records in <i>cms_module_visitorstats</i><br>”;
}

mysql_close($conn);

$tf=microtime();
echo “ENDOF SQL EXECUTION: “. $tf .”\n<br>\n”;
echo “TIME ELAPSED  “. (1000 * number_format(diff_microtime($ti, $tf), 3) ).” milliseconds”;
?>

Step 2: I wrote a shell script on freeshell.eu in order to call from crontab my php remote script and writing my measures on a log file. My crontab looks like this:

1,11,21,31,41,51 * * * * bin/getdata.sh

and my shell script, is simply:

#!/bin/sh
OUTFILE=”public_html/test/sqlprau.log”
cd
touch $OUTFILE
MILLI=`lynx -dump http://www.mywebsite.org/test.php|grep “TIME ELAPSED” | awk ‘{ print $3}’`
SECON=`date “+%d/%m/%Y %H:%M:%S – %s – “`
echo $SECON $MILLI >> $OUTFILE

Step 3: Thanks to perl and gnuplot programs, getting some help from this manual, I was able to write this simple perl cgi-bin to read the file made in the  step 2 and show a sort of graphic like the following.
Yes, now I’m sure my website appears to be slow.

how my server is slow

No such directory error calling lynx in a shell script

Monday, June 14th, 2010

Some time ago I was testing a shell script nagios plugin. Running this script from command line was ok, but once called from nagios scheduling, the plugin standard error was

/root/: No such directory

Due to some little problem (is this a lynx problem?)  I needed to set the HOME environment variable before calling lynx.

MYSTR=`export HOME=/tmp && /usr/bin/lynx -dump “http://$MYHOST/$MYURL?MYFILTER=$MYPARAM”`

not – integer number comparison

Monday, March 8th, 2010

Some day ago I needed a shell script to compare some decimal values, but it’s not possible to compare as usual

MAX=33.33
if [ $MYVALUE -gt $MAX ]; then ….

Running something like this will bring the error message “integer expression expected!”.

With a little help from bc, it’s possible to compare non decimal values. Here follows a simple unuseful nagios plugin to compare decimal values:

#!/bin/bash

MYVALUE=$1
MAXWARN=”82.5″
MAXCRIT=”91.3″
MYDESCR=”Value description”
RET_OK=”0″
RET_WARN=”1″
RET_CRIT=”2″
RET_UNKN=”3″

PERFDATAMSG=”|’$MYDESCR’=$MYVALUE;$MAXWARN;$MAXCRIT”

if [ $(echo “$MYVALUE > $MAXCRIT”|bc) -gt 0 ]; then
echo “ERROR – $MYDESCR=$MYVALUE$PERFDATAMSG”
exit $RET_CRIT
fi

if [ $(echo “$MYVALUE > $MAXWARN”|bc) -gt 0 ]; then
echo “WARNING – $MYDESCR=$MYVALUE$PERFDATAMSG”
exit $RET_WARN
else
echo “OK – $MYDESCR=$MYVALUE$PERFDATAMSG”
exit $RET_OK
fi