Cleand up code and completed check and warning/critical check

This commit is contained in:
Jack-Benny Persson 2013-04-14 04:41:39 +02:00
parent e038912a38
commit b0c17192d0
2 changed files with 38 additions and 8 deletions

Binary file not shown.

View File

@ -1,20 +1,25 @@
#!/usr/bin/php5 #!/usr/bin/php5
<?php <?php
//include class file //include PEAR ConsoleGetopt
include ("Console/Getopt.php"); include ("Console/Getopt.php");
$VERSION="1.1"; $VERSION="1.1";
$AUTHOR="(c) 2013 Jack-Benny Persson (jack-benny@cyberinfo.se)"; $AUTHOR="(c) 2013 Jack-Benny Persson (jack-benny@cyberinfo.se)";
// Exit codes // Exit codes
$STATE_OK=0; $STATE_OK=0;
$STATE_WARNING=1; $STATE_WARNING=1;
$STATE_CRITICAL=2; $STATE_CRITICAL=2;
$STATE_UNKNOWN=3; $STATE_UNKNOWN=3;
// Functions
// Default to critical
$warning = "no";
// Functions
function print_version() function print_version()
{ {
global $argv, $argc; global $argv, $argc;
@ -50,6 +55,8 @@ EOD;
echo "\n$HELP_TEXT\n"; echo "\n$HELP_TEXT\n";
} }
// Arguments and options (depending on PEAR ConsoleGetopt)
$options = new Console_Getopt(); $options = new Console_Getopt();
$shortoptions = "hV"; $shortoptions = "hV";
@ -82,7 +89,6 @@ if(sizeof($opts) > 0)
case '--file': case '--file':
$filename = $o[1]; $filename = $o[1];
echo $filename . "\n";
break; break;
case '--md5': case '--md5':
@ -90,12 +96,14 @@ if(sizeof($opts) > 0)
break; break;
case '--warning': case '--warning':
echo ""; $warning = "yes";
break; break;
} }
} }
} }
// Sanity checks
if (empty($filename)) if (empty($filename))
{ {
fwrite(STDERR,"A filename is requierd\n"); fwrite(STDERR,"A filename is requierd\n");
@ -107,14 +115,36 @@ if (empty($md5))
fwrite(STDERR,"You need to enter an MD5 checksum\n"); fwrite(STDERR,"You need to enter an MD5 checksum\n");
exit($STATE_UNKNOWN); exit($STATE_UNKNOWN);
} }
// MAIN
// Compare the file against the MD5 checksum
$file = md5_file($filename); $file = md5_file($filename);
if ($file == $md5) if ($file == $md5) // Checksum is ok
{ {
echo "They match\n"; fwrite(STDOUT, "$filename has a corect MD5 checksum\n");
exit($STATE_OK);
} }
echo $file;
echo "\n\n"; elseif ($file != $md5) // Checksum is not ok
{
fwrite(STDERR, "$filename does NOT match MD5 checksum\n");
if ($warning == "yes") // Fail as warning
{
exit($STATE_WARNING);
}
elseif ($warning == "no") // Fail as critical
{
exit($STATE_CRITICAL);
}
}
else // Fail as unknown, something went haywire
{
fwrite(STDERR, "Unkown state\n");
exit($STATE_UNKNOWN);
}
?> ?>