commit b504912e9d49b10b7c4d276a91cb682d9b480c06 Author: Jack-Benny Persson Date: Wed Dec 10 11:27:42 2014 +0100 Initial commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..80983d3 --- /dev/null +++ b/LICENSE @@ -0,0 +1,15 @@ +Copyright (C) 2014 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 diff --git a/README.md b/README.md new file mode 100644 index 0000000..09bce30 --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +# check\_temp.py # +Another simple Nagios plugin to check the temperature with lm-sensors. +This one is written in Python and can be used to monitor any adapter the user +specifies with the -a option. + +## Usage ## + usage: check_temp.py [-h] -w N -c N -a string + + optional arguments: + -h, --help show this help message and exit + -w N warning temperature + -c N critical temperature + -a string the adapter to monitor (for example: acpitz-virtual-0) + +## License ## +check\_temp.py is relased under GNU GPL. diff --git a/check_temp.py b/check_temp.py new file mode 100755 index 0000000..fb615cb --- /dev/null +++ b/check_temp.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +import subprocess +import sys +import argparse + +def check_temp(warn, crit, adapter): + """ + This is a simple Nagios plugin to + check the temperature with lm-sensors and then + returning the correct return code to main. + """ + + # Use lm-sensors to check the temperature + try: + sensors_output = subprocess.check_output(['sensors']) + except OSError: + print "Couldn't execute sensors (from lm-sensors)" + sys.exit(3) + + # Grab the temperature for the specified adapter by splitting the string + # into a list and then getting the index number for the adapter name and + # adding 5 to the index number. + # Lastly, we strip away the + sign and the degree symbols and typecast it + # to a float + sensors_output = sensors_output.split() + t = sensors_output.index(adapter) + 5 + temp = sensors_output[t] + temp = temp[1:-3] + temp = float(temp) + + if warn > crit: + print "Warning temp must be less than critical temp" + sys.exit(3) + + # Do the actual temperature check/comparison. + # For each case we return both the code and the current temperature + if temp < warn: + return 0, temp + + elif temp > warn and temp < crit: + return 1, temp + + elif temp >= crit: + return 2, temp + + else: + return 3, temp + + +def main(): + + # Parse command line options with argparse + cli_parser = argparse.ArgumentParser() + cli_parser.add_argument('-w', metavar='N', type=float, required=True, + help='warning temperature') + cli_parser.add_argument('-c', metavar='N', type=float, required=True, + help='critical temperature') + cli_parser.add_argument('-a', metavar='string', type=str, required=True, + help='the adapter to monitor (for example: acpitz-virtual-0)') + args = cli_parser.parse_args() + + # Get the return-code and temperature from check_temp function + return_code, temp = (check_temp(args.w, args.c, args.a)) + + # Print temperature and message and then exit with the correct return code + if return_code == 0: + print ("OK - Temperature is " + str(temp)) + sys.exit(0) + elif return_code == 1: + print ("WARNING - Temperature is " + str(temp)) + sys.exit(1) + elif return_code == 2: + print ("CRITICAL - Temperature is " + str(temp)) + sys.exit(2) + else: + print ("UNKNOWN - Something went wrong with " + sys.argv[0] + "!") + sys.exit(3) + +if __name__=='__main__': + main()