diff --git a/check_cpu.py/nagios-okplugin-check_cpu.spec b/check_cpu.py/nagios-okplugin-check_cpu.spec new file mode 100644 index 0000000..2f10f91 --- /dev/null +++ b/check_cpu.py/nagios-okplugin-check_cpu.spec @@ -0,0 +1,58 @@ +%define debug_package %{nil} + +Summary: A Nagios plugin to check CPU on Linux servers +Name: nagios-okplugin-check_cpu +Version: 1.0 +Release: 1%{?dist} +License: GPLv2+ +Group: Applications/System +URL: https://github.com/opinkerfi/nagios-plugins/ +Source0: https://github.com/opinkerfi/nagios-plugins/archive/%{name}-%{version}-%{release}.tar.gz +Requires: nagios-okplugin-common +BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n) +Packager: Tomas Edwardsson +BuildArch: noarch + +%description +Check cpu states on line machines + +%prep +%setup -q + +%build + + +%install +rm -rf %{buildroot} +mkdir -p %{buildroot}%{_sharedstatedir}/nagios-okplugin/%{name} +install -D -p -m 0755 okplugin_check_cpu %{buildroot}%{_libdir}/nagios/plugins/okplugin_check_cpu +install -D -p -m 0755 nrpe.d/okplugin_check_cpu.cfg %{buildroot}/etc/nrpe.d/okplugin_check_cpu.cfg + + +%clean +rm -rf %{buildroot} + +%post +/sbin/service nrpe reload + +%files +%defattr(-,root,root,-) +%dir %attr(0770, nrpe, nrpe) %{_sharedstatedir}/nagios-okplugin/%{name} +%{_libdir}/nagios/plugins/* +%{_sysconfdir}/nrpe.d/* + + + +%changelog +* Thu Aug 23 2012 Pall Sigurdsson 1.0-1 +- Version number bumped +- Updates buildarch to noarch (tommi@tommi.org) + +* Mon Mar 12 2012 Pall Sigurdsson 0.3-1 +- new package built with tito + +* Thu Nov 25 2010 Pall Sigurdsson 0.1-2 +- Nrpe config now ships with plugin by default +* Mon Mar 1 2010 Tomas Edwardsson 0.1-1 +- Initial packaging + diff --git a/check_cpu.py/nrpe.d/okplugin_check_cpu.cfg b/check_cpu.py/nrpe.d/okplugin_check_cpu.cfg new file mode 100644 index 0000000..319783c --- /dev/null +++ b/check_cpu.py/nrpe.d/okplugin_check_cpu.cfg @@ -0,0 +1 @@ +command[okplugin_check_cpu]=/usr/lib/nagios/plugins/okplugin_check_cpu diff --git a/check_cpu.py/okplugin_check_cpu b/check_cpu.py/okplugin_check_cpu new file mode 100644 index 0000000..3230c92 --- /dev/null +++ b/check_cpu.py/okplugin_check_cpu @@ -0,0 +1,91 @@ +#!/usr/bin/python +# +# Copyright 2014, Tomas Edwardsson +# +# 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 3 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, see . +""" +Gathers information on cpu usage on a linux machine +""" + +from pynag.Plugins import PluginHelper, ok, unknown + + +def main(): + """The main logic of the program""" + + # See man proc and search for /proc/stat + stat_fields = ['cpu', 'user', 'nice', 'system', 'idle', 'iowait', 'irq', + 'softirq', 'steal', 'guest', 'guest_nice'] + + pyplug = PluginHelper() + + old_stat = get_saved_stat() + stat = get_stat() + + # Find shortest stat + stat_length = len(old_stat) > len(stat) and len(old_stat) or len(stat) + + if old_stat: + diff = [] + for pos in range(stat_length): + diff.append(stat[pos] - old_stat[pos]) + averages = calc_avg(diff) + else: + averages = calc_avg(stat) + + for pos in range(stat_length): + pyplug.add_metric(label=stat_fields[pos+1], value=averages[pos], + uom="%") + + try: + save_stat(stat) + except IOError, err: + pyplug.exit(unknown, "Unable to save cpu statistics: %s" % err) + + pyplug.add_status(ok) + pyplug.add_summary("CPU load %.2f%%" % (100.0 - averages[3])) + pyplug.exit() + +def calc_avg(stat): + """Calculates the average of each int in array""" + stat_sum = sum(stat) + avg = [] + for pos in range(len(stat)): + avg.append(float(stat[pos]) / stat_sum * 100) + return avg + +def get_stat(): + """Fetches the first line from /proc/stat and returns the numbers""" + stat_fh = open("/proc/stat") + return [int(pos) for pos in stat_fh.readline().split()[1:]] + +def get_saved_stat(): + """Fetches information about the last run to be able to do averages""" + try: + stat_fh = open("/var/lib/nagios-okplugin/check_cpu") + except IOError: + return None + return [int(pos) for pos in stat_fh.readline().split()[1:]] + +def save_stat(stat): + """Save statistics to compare to in next run""" + stat_fh = open("/var/lib/nagios-okplugin/check_cpu", "w") + + stat_fh.write("cpu " + " ".join([str(pos) for pos in stat]) + "\n") + stat_fh.close() + +if __name__ == "__main__": + main() + +# vim: sts=4 expandtab