Rewritten command line parsing code to get rid of Console_GetoptPlus dependency
This commit is contained in:
parent
a6d0484bec
commit
413a92ec66
8
CHANGELOG
Normal file
8
CHANGELOG
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
Version 2.0
|
||||||
|
Rewritten the entire command line parsing code to get rid of the
|
||||||
|
PEAR Console_GetoptPlus dependency.
|
||||||
|
The script sould now be able to run out-of-the-box on most
|
||||||
|
machines without the need for Console_GetoptPlus.
|
||||||
|
|
||||||
|
Version 1.0
|
||||||
|
First version released!
|
6
README
6
README
@ -8,12 +8,6 @@ This is a re-write of my previous check_md5 that was written in Bash. The Bash
|
|||||||
version is more tested and should work better. I wrote this this version mainly
|
version is more tested and should work better. I wrote this this version mainly
|
||||||
as a way to practice some PHP.
|
as a way to practice some PHP.
|
||||||
|
|
||||||
NOTE: For this script to work you need PEAR Console_GetoptPlus (or
|
|
||||||
Console_Getopt).
|
|
||||||
See http://pear.php.net/package/Console_GetoptPlus for more information on how
|
|
||||||
to install Console_GetoptPlus.
|
|
||||||
|
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
-h
|
-h
|
||||||
Print detailed help screen
|
Print detailed help screen
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
#!/usr/bin/php
|
#!/usr/bin/php
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
/*
|
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
# #
|
# #
|
||||||
# Copyright (C) 2013 Jack-Benny Persson <jack-benny@cyberinfo.se> #
|
# Copyright (C) 2013 Jack-Benny Persson <jack-benny@cyberinfo.se> #
|
||||||
@ -28,18 +26,13 @@
|
|||||||
# Nagios plugin to monitor a single files MD5 sum. In case of mismatch #
|
# Nagios plugin to monitor a single files MD5 sum. In case of mismatch #
|
||||||
# the plugin exit with a CRICITAL error code. This behavior can be changed #
|
# the plugin exit with a CRICITAL error code. This behavior can be changed #
|
||||||
# with the --warning argument. #
|
# with the --warning argument. #
|
||||||
# Rewritten in PHP (depending on PEAR ConsoleGetopt. #
|
# Rewritten in PHP. #
|
||||||
# #
|
# #
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
//include PEAR ConsoleGetopt
|
$VERSION="2.0";
|
||||||
include ("Console/Getopt.php");
|
$AUTHOR="(c) 2014 Jack-Benny Persson (jack-benny@cyberinfo.se)";
|
||||||
|
|
||||||
|
|
||||||
$VERSION="1.0";
|
|
||||||
$AUTHOR="(c) 2013 Jack-Benny Persson (jack-benny@cyberinfo.se)";
|
|
||||||
|
|
||||||
// Exit codes
|
// Exit codes
|
||||||
$STATE_OK=0;
|
$STATE_OK=0;
|
||||||
@ -89,58 +82,38 @@ EOD;
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Arguments and options (depending on PEAR ConsoleGetopt)
|
// Command line parsing
|
||||||
$options = new Console_Getopt();
|
|
||||||
|
|
||||||
$shortoptions = "hV?";
|
$shortoptions = "hV?";
|
||||||
$longoptions = array("warning", "help", "version", "file=", "md5=");
|
$longoptions = array("warning", "help", "version", "file:", "md5:");
|
||||||
|
|
||||||
$args = $options->readPHPArgv();
|
$options = getopt($shortoptions, $longoptions);
|
||||||
$ret = $options->getopt($args, $shortoptions, $longoptions);
|
|
||||||
|
|
||||||
if (PEAR::isError($ret))
|
if (isset($options['h']) || isset($options['?']) || isset($options['help']))
|
||||||
{
|
{
|
||||||
fwrite(STDERR,$ret->getMessage() . "\n\n");
|
print_help();
|
||||||
print_help();
|
exit ($STATE_OK);
|
||||||
exit ($STATE_UNKNOWN);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$opts = $ret[0];
|
if (isset($options['V']) || isset($options['version']))
|
||||||
|
|
||||||
if(sizeof($opts) > 0)
|
|
||||||
{
|
{
|
||||||
foreach($opts as $o)
|
print_version();
|
||||||
{
|
exit ($STATE_OK);
|
||||||
switch($o[0])
|
|
||||||
{
|
|
||||||
case 'h':
|
|
||||||
case '--help':
|
|
||||||
case '?':
|
|
||||||
print_help();
|
|
||||||
exit ($STATE_OK);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'V':
|
|
||||||
case '--version':
|
|
||||||
print_version();
|
|
||||||
exit ($STATE_OK);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case '--file':
|
|
||||||
$filename = $o[1];
|
|
||||||
break;
|
|
||||||
|
|
||||||
case '--md5':
|
|
||||||
$md5 = $o[1];
|
|
||||||
break;
|
|
||||||
|
|
||||||
case '--warning':
|
|
||||||
$warning = "yes";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isset($options['file']))
|
||||||
|
{
|
||||||
|
$filename = $options['file'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($options['md5']))
|
||||||
|
{
|
||||||
|
$md5 = $options['md5'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($options['warning']))
|
||||||
|
{
|
||||||
|
$warning = "yes";
|
||||||
|
}
|
||||||
|
|
||||||
// Sanity checks
|
// Sanity checks
|
||||||
if (empty($filename))
|
if (empty($filename))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user