From d322845589b6802c75aae2e3e7c410938ab80a3c Mon Sep 17 00:00:00 2001 From: Jack-Benny Persson Date: Fri, 15 Aug 2014 10:59:19 +0200 Subject: [PATCH] Added check to see if cURL could fetch the XML-file If cURL fails in retrieving the XML-file, then exit with a UNKNOWN state. --- HISTORY | 5 +++++ check_smhiwarn.php | 13 +++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/HISTORY b/HISTORY index 4d189b9..bc193ba 100644 --- a/HISTORY +++ b/HISTORY @@ -1,3 +1,8 @@ +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} diff --git a/check_smhiwarn.php b/check_smhiwarn.php index 3f6ec21..b190d11 100755 --- a/check_smhiwarn.php +++ b/check_smhiwarn.php @@ -26,7 +26,7 @@ define ("CRITICAL", 2); define ("UNKNOWN", 3); // Define version and program -define ("VERSION", 0.4); +define ("VERSION", 0.5); define ("PROGRAM", $argv[0]); // Function for printing usage @@ -129,9 +129,18 @@ if (!preg_grep("/^$district$/u", $availDistricts)) $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); -$data = curl_exec($ch); +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) + // 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", $data, $matches);