commit 24737bc93c835f661bb6656b9b0c2467a764b307 Author: Jack-Benny Persson Date: Fri Jan 13 06:09:08 2017 +0100 First commit of check_meteoalarm.php diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..4ed9462 --- /dev/null +++ b/LICENSE @@ -0,0 +1,15 @@ +Copyright (C) 2017 Jack-Benny Persson + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..6b0007c --- /dev/null +++ b/README.md @@ -0,0 +1,50 @@ +# check_meteoalarm.php + +This is a Nagios plugin written in PHP to look for weather alerts on the site +http://www.meteoalarm.eu. The plugin checks a specific region, which is entered +as the full URL of the region as an argument to the script. + +The script is released under GNU GPL and hence I can take no responsibility for +the correctness of the script. It might not work as expected and should not be +be used as a critical tool for weather alerts. + +## Requirements + +PHP version 5 or higher with cURL. To install these packages on Debian/Ubuntu +enter `sudo apt-get install php5-cli php-curl`. That should be enough to get the +script running. + +## Usage + + ./check_meteoalarm.php [FULL URL] + +For example + + ./check_meteoalarm.php http://www.meteoalarm.eu/en_UK/0/0/SE002-Sk%E5ne.html + +Note that the URL must be the english version (en_UK), otherwise the script +can't regex for the correct strings. + +### Usage within Nagios (on Debian systems) + +**Step 1:** Place the script in `/usr/lib/nagios/plugins/` + +**Step 2:** Create the following snippet in `/etc/nagios3/commands.cfg` + + define command{ + command_name check_meteoalarm + command_line /usr/lib/nagios/plugins/check_meteoalarm.php $ARG1$ + } + +**Step 3:** Create a service definition for a region, for example in your localhost in + `/etc/nagios3/conf.d/localhost_nagios2.cfg` + + define service{ + use generic-service + host_name localhost + service_description Meteoalarm Skane + check_interval 15 + retry_interval 3 + check_command check_meteoalarm!'http://www.meteoalarm.eu/en_UK/0/0/SE002-Sk%E5ne.html' + } + diff --git a/check_meteoalarm.php b/check_meteoalarm.php new file mode 100644 index 0000000..1a480ab --- /dev/null +++ b/check_meteoalarm.php @@ -0,0 +1,93 @@ +#!/usr/bin/env php + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +/* + This is a small Nagios plugin to warn for severe weather. It gets the data + from meteoalarm.eu. The script takes the full URL of the region your are + interested in. For example: + ./check_meteo.php www.meteoalarm.eu/en_UK/0/0/SE002-Sk%E5ne.html +*/ + +// Sanity check & help text +if ($argc != 2) +{ + printf("Please provide a full URL for your region\n"); + printf("Usage:\n"); + printf("$argv[0] [URL]\n\n"); + printf("Please note that you must use the english version of the site/URL\n\n"); + printf("Example:\n"); + printf("$argv[0] http://www.meteoalarm.eu/en_UK/0/0/SE038-Halland.html\n"); + exit(3); +} + +// Get HTML-file from Meteoalarm +$url = $argv[1]; +$ch = curl_init($url); +curl_setopt($ch, CURLOPT_HEADER, 0); +curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); +$data = curl_exec($ch); +curl_close($ch); + +// Get all the warnings for the selected region +preg_match_all("/class=\"text\"\>(.*) /i", $data, $warnMatches); + +// If no warnings were found, check for the text "No special awarenes required" +// to make sure we're actually scanning a Meteoalarm page. If we're not, print +// an error message about it and exit with code 3 (UNKNOWN). +if (empty($warnMatches[1])) +{ + if(preg_match("/No special awareness required/", $data)) + { + printf("OK - No warnings for the region\n"); + exit(0); // Code 0 = OK + } + else + { + printf("UNKNOWN - That doesn't look like a Meteolarm page, please check the URL.\n"); + printf("Make sure you are using the english version of the site.\n"); + exit(3); // Code 3 = Unknown + } +} + +// There is a warning of some kind... +else +{ + // Get the severity of the warning (orange & red = high danger, + // yellow = low danger) + if (preg_match("/Orange/", $data) || preg_match("/Red/", $data)) + { + printf("CRITICAL - "); // Start the message with "CRITICAL" + // Loop through all the warnings and print them + foreach($warnMatches[1] as $warn) + printf($warn . "\n"); + exit(2); // Code 2 = Critical + } + if (preg_match("/Yellow/", $data)) + { + printf("WARNING - "); // Start the message with "WARNING" + // Loop through all the warnings and print them + foreach($warnMatches[1] as $warn) + printf($warn . "\n"); + exit(1); // Code 1 = Warning + } + +} + + +?> +