#!/usr/bin/php * Homepage: http://nagios.com/ * Description: Checks for updates to the Nagios XI monitoring software, * and alert if the running version does not match the * latest release, with thresholds based on the age of the * new release so admins can define how quickly they need to * apply updates to the software. * * Revision history is kept in private Subversion. * * Usage: ./check_xi_updates [-w ] [-c ] * e.g. ./check_xi_updates -w 7 -c 14 * e.g. ./check_xi_updates -c 3 * * ----------------------------------------------------------------------------- * * Full license text: * * Nagios Open Software License Version 1.2 * * Copyright (c) 2010 Nagios Enterprises, LLC * * Nagios Enterprises, LLC (the "Company") hereby grants, free of charge, to any * person, company, or legal entity (each a "User") obtaining a copy of this * software and associated documentation files (the "Software"), to deal in the * Software without restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software (each a "Use"), and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * i. The above copyright notice and this license shall be included in its * entirety in all copies or substantial portions of the Software. * * ii. The copyright notices and attribution information included in the * Software may not be altered or removed. * * iii. The Software may not be Forked. "Forking" and "to Fork" means to create * or distribute a product or a derivative work of the object or source code of * the Software under a new or different brand. * * iv. Any Use of the Software is subject to compliance with the terms and * conditions of the Nagios Trademark Use Restrictions, which are incorporated * herein by reference and can be viewed online at * http://www.nagios.com/legal/trademarks/ * * v. Any Contribution of materials to the Software by any User is subject to, * and signifies, either explicitly or implicitly, the User's acceptance of the * terms and conditions of the Nagios Contributor Agreement, which are * incorporated herein by reference and can be viewed online at * http://www.nagios.com/legal/contributoragreement/. "Contribution" means any * source code, object code, patch, tool, sample, graphic, specification, * manual, documentation, or any other material posted or submitted by a User to * the Company, directly or indirectly, via the Company's websites, forums, * email lists, or other methods of communication. * * vi. This license is not intended to restrict any other rights that may be * granted to you by other software and associated documentation files that may * be included in the Software. If the Software is considered to be a * collective work under U.S. copyright laws, the terms of this license do not * apply to other contributions of the collective work unless stated otherwise. * * vii. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM FOR DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, * OR PROFITS; OR BUSINESS INTERRUPTION) OR OTHER LIABILITY, WHETHER IN AN * ACTION OF CONTRACT, STRICT LIABILITY, TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) OR OTHER ACTION, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * * viii. The terms of this license are subject to the User's compliance with all * terms of this license. In the event that any term or condition is violated * by the User, the terms of this license terminate and the User shall not have * any rights they otherwise might have been granted under this license. * * ============================================================================= */ function usage() { echo "\n"; echo "Name: check_xi_updates\n"; echo "Description: Nagios XI software update check\n"; echo "Author: Tony Yarusso \n"; echo "Copyright: 2011, Nagios Enterprises LLC.\n"; echo "License Nagios Open Software License \n"; echo "\n"; echo "Usage: check_xi_updates [-w ] [-c ]\n"; echo "e.g. ./check_xi_updates -w 7 -c 14\n"; echo "e.g. ./check_xi_updates -c 3\n"; echo "( refers to the number of days since the last XI release.)\n"; echo "\n"; echo "Options:\n"; echo " -h, --help\n"; echo " Print detailed help screen\n"; echo " -w, --warning\n"; echo " Minimum release age in days to trigger a warning state " . "[Default=7]\n"; echo " -c, --critical\n"; echo " Minimum release age in days to trigger a critical state ". "[Default=14]\n"; exit(0); } // Implement standard "help" switch for usage information if ((in_array("-h", $argv)) or (in_array("--help", $argv))) { usage(); } // Process arguments $arglen = count($argv); $i = 1; while ($i < $arglen) { switch ($argv[$i]) { case "-w": if (isset($argv[($i+1)])) { if (intval($argv[($i+1)])) { $WARN = $argv[($i+1)]; $i++; } else { echo "\nThresholds should be a number of days as a " . "natural number.\n"; usage(); } } else { echo "\nThe -w switch requires a value.\n"; usage(); } break; case "-c": if (isset($argv[($i+1)])) { if (intval($argv[($i+1)])) { $CRIT = $argv[($i+1)]; $i++; } else { echo "\nThresholds should be a number of days as a " . "natural number.\n"; usage(); } } else { echo "\nThe -c switch requires a value.\n"; usage(); } break; default: echo "\nUnknown option: " . $argv[$i] . "\n"; usage(); } $i++; } // Check threshold sanity if (isset($WARN) && isset($CRIT)) { if ($WARN > $CRIT) { echo "\nWarning value can not be greater than critical value.\n"; usage(); } } else { if (isset($WARN) || isset($CRIT)) { if (!isset($WARN)) { if ($CRIT >= 7) { $WARN = 7; } else { $WARN = $CRIT; } } elseif (!isset($CRIT)) { if ($WARN <= 14) { $CRIT = 14; } else { $CRIT = $WARN; } } } else { $WARN = 7; $CRIT = 14; } } // Include relevant XI libraries require_once("/usr/local/nagiosxi/html/includes/common.inc.php"); // Connect to the XI database (where optiosn are stored) db_connect(DB_NAGIOSXI); db_connect(DB_NDOUTILS); // Run the built-in update check process do_update_check(true); // Fetch new stored values from the database $update_available = get_option("update_available"); $update_version = get_option("update_version"); $release_date = strtotime(get_option("update_release_date")); $release_notes = get_option("update_release_notes"); $check_succeeded = get_option("last_update_check_succeeded"); $check_time = get_option("last_update_check_time"); $product_name = get_product_name(true); $product_version = get_product_version(); $product_build = get_product_build(); // Format dates into useful types $build_date_array = strptime($product_build, '%Y%m%d'); $build_year = (1900 + $build_date_array['tm_year']); if ($build_date_array['tm_mon'] <= 8) { $build_month = "0" . (1 + $build_date_array['tm_mon']); } else { $build_month = (1 + $build_date_array['tm_mon']); } if ($build_date_array['tm_mday'] <= 8) { $build_day = "0" . $build_date_array['tm_mday']; } else { $build_day = $build_date_array['tm_mday']; } $build_date = strtotime($build_year . "-" . $build_month . "-" . $build_day); $todays_date = strtotime(date("Y-m-d"));; // Calculate number of days since running & latest releases came out $build_age = floor( ($todays_date - $build_date) / (60*60*24) ); $release_age = floor( ($todays_date - $release_date) / (60*60*24) ); // Set state & message based on loaded values if ($check_succeeded != 1) { $message = "Failed to check for updates."; $status = 3; $status_text = "XI Updates UNKNOWN"; $build_age = 0; $release_age = 0; } else { if ($update_available) { $message = "New XI version (" . $update_version . ") available since " . date('F jS, Y', $release_date) . ". Currently running " . $product_version . " (" . $build_age . " days old)."; // Admins might have different standards for when it's acceptable to // apply patches, so handle thresholds for number of days release is old if ($release_age >= $CRIT) { $status = 2; $status_text = "XI Updates CRITICAL"; } elseif ($release_age >= $WARN) { $status = 1; $status_text = "XI Updates WARNING"; } else { $status = 0; $status_text = "XI Updates OK"; } } else { $message = "XI version is up to date. " . $product_version . ", released " . date('F jS, Y', $release_date) . " (" . $release_age . " days ago)."; $status = 0; $status_text = "XI Updates OK"; $build_age = 0; } } // Provide plugin output with performance data echo $status_text . ": " . $message . "|'Build Age'=" . $build_age . ";;;0;730 'Release Age'=" . $release_age . ";;;0;730\n"; // Send Nagios a status return code exit($status);