Compare commits

..

No commits in common. "master" and "v0.1" have entirely different histories.
master ... v0.1

3 changed files with 25 additions and 63 deletions

21
HISTORY
View File

@ -1,21 +0,0 @@
0.5 - 2014-08-15
Added a check to see if cURL was successful in
retrieving the XML-file. If it was not, then exit
with a UNKNOWN state and print the cURL error message.
0.4 - 2014-08-09
Added /u modifier to more preg_matches for UTF-8
Replaced a-zåäö with \p{L}
Fixed a couple of spelling errors and missing words
Replaced $program with PROGRAM on line 122
0.3 - 2014-08-03
Added /u modifier to the preg_match for UTF-8
0.2 - 2014-07-31
Replaced 'shell_exec("curl -s URL")' with PHP cURL function
Replaced all state-variables with constants
Replaced version and program variables with constants
0.1 - 2014-07-27
First release of check_smhiwarn

View File

@ -16,9 +16,6 @@ Don't forget to quote the district in the argument, such as the below example.
Create one instance of each district you want to monitor.
## Requirements ##
The script requires PHP5 and the PHP5 cURL module (php5-curl on Debian systems).
## Copyright ##
Original author is Jack-Benny Persson (jack-benny@cyberinfo.se).

View File

@ -20,22 +20,21 @@
*/
// Define exit status
define ("OK", 0);
define ("WARNING", 1);
define ("CRITICAL", 2);
define ("UNKNOWN", 3);
$ok = 0;
$warning = 1;
$critical = 2;
$unknown = 3;
// Define version and program
define ("VERSION", 0.5);
define ("PROGRAM", $argv[0]);
$version = 0.1;
$program = $argv[0];
// Function for printing usage
function usage()
{
print "check_smhiwarn version " . VERSION . "\n";
print "check_smhiwarn version $GLOBALS[version]\n";
print "Copyright (C) Jack-Benny Persson <jack-benny@cyberinfo.se>\n";
print "Usage: " . PROGRAM . " 'District'\n";
print "Example: " . PROGRAM . " 'Skåne län utom Österlen'\n";
print "Usage: $GLOBALS[program] 'District'\n";
print "Example: $GLOBALS[program] 'Skåne län utom Österlen'\n";
}
// All of the avaliable districts
@ -101,7 +100,7 @@ $availDistricts = array(
if (!isset($argv[1]))
{
usage();
exit(UNKNOWN);
exit($unknown);
}
// Set first argument to $district
@ -112,46 +111,33 @@ if ($district == 'list')
{
foreach ($availDistricts as $dist)
print "$dist\n";
exit(UNKNOWN);
exit($unknown);
}
// Check if the district exists
if (!preg_grep("/^$district$/u", $availDistricts))
if (!preg_grep("/^$district$/", $availDistricts))
{
print "$district does not exists\n";
print "List all avaliable districts by \"" . PROGRAM . " 'list'\"\n";
exit(UNKNOWN);
print "List all avaliable districts by \"$program 'list'\"\n";
exit($unknown);
}
// Retrive the data
//$data = file_get_contents("testing/smhi_alla_varningar.xml"); //For testing purposes
$ch = curl_init("http://www.smhi.se/weatherSMHI2/varningar/smhi_alla_varningar.xml");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
// check if curl failed and bail out if it did
if (($data = curl_exec($ch)) === false)
{
print curl_error($ch);
curl_close($ch);
exit(UNKNOWN);
}
curl_close($ch);
//print "\n$data\n"; // for testing purposes (uncomment to view xml-file)
$data = shell_exec("curl -s http://www.smhi.se/weatherSMHI2/varningar/smhi_alla_varningar.xml");
// Regex the area (1st paranthesis is area, 2nd is warning class, 3rd is warning msg)
preg_match("/($district)(?:: )(?:Varning klass )([1-3]+)(?:,\s)([-0-9\p{L}.,&\s]*)/iu",
preg_match("/($district)(?:: )(?:Varning klass )([1-3]+)(?:,\s)([-a-z0-9åäö.,&\s]*)/i",
$data, $matches);
// Count how many warnings are issued and issue a critical if more than one
preg_match_all("/$district/u", $data, $counts);
preg_match_all("/$district/", $data, $counts);
$numberMatches = (count($counts[0]));
if ($numberMatches > 1)
{
print "More than one warning is issued for $district, check smhi.se!";
exit(CRITICAL);
print "More than one warning are issued for $district, check smhi.se!";
exit($critical);
}
// Define the paranthesis
@ -169,20 +155,20 @@ else
switch ($warnLevel)
{
case 0:
print "No warnings issued for $district";
exit(OK);
print "No warnings issued $district";
exit($ok);
case 1:
print "Class 1 warning issued for $district: $warnMsg";
exit(WARNING);
exit($warning);
case 2:
print "Class 2 warning issued for $district: $warnMsg";
exit(CRITICAL);
exit($critical);
case 3:
print "Class 3 warning issued for $district: $warnMsg";
exit(CRITICAL);
exit($critical);
default:
print "Unknown error for $district";
exit(UNKNOWN);
exit($unknown);
}
?>