From be7032f4e408a2e26fa7d16d0ef0d7ac5e90fdce Mon Sep 17 00:00:00 2001 From: Tomas Edwardsson Date: Mon, 30 Dec 2013 20:30:35 +0000 Subject: [PATCH 01/13] Added fedora 19 --- rel-eng/releasers.conf | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/rel-eng/releasers.conf b/rel-eng/releasers.conf index 103f1e5..0ff8529 100644 --- a/rel-eng/releasers.conf +++ b/rel-eng/releasers.conf @@ -1,3 +1,18 @@ +# Fedora FC18 +[test-fc19-x86_64] +releaser = tito.release.YumRepoReleaser +builder = tito.builder.MockBuilder +builder.mock = fedora-19-x86_64 +builder.test = 1 +rsync = tito@opensource.is:/var/www/sites/opensource.ok.is/repo/testing/fedora19/x86_64/ + + +[production-fc19-x86_64] +releaser = tito.release.YumRepoReleaser +builder = tito.builder.MockBuilder +builder.mock = fedora-19-x86_64 +rsync = tito@opensource.is:/var/www/sites/opensource.ok.is/repo/fedora19/x86_64/ + # Fedora FC18 [test-fc18-x86_64] releaser = tito.release.YumRepoReleaser From 63f285a037e0b66f3d988eb5b539b5f3fc4c5836 Mon Sep 17 00:00:00 2001 From: Tomas Edwardsson Date: Mon, 30 Dec 2013 20:31:22 +0000 Subject: [PATCH 02/13] Added Fedora 20 --- rel-eng/releasers.conf | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/rel-eng/releasers.conf b/rel-eng/releasers.conf index 0ff8529..9ed2c62 100644 --- a/rel-eng/releasers.conf +++ b/rel-eng/releasers.conf @@ -1,4 +1,19 @@ -# Fedora FC18 +# Fedora FC20 +[test-fc20-x86_64] +releaser = tito.release.YumRepoReleaser +builder = tito.builder.MockBuilder +builder.mock = fedora-20-x86_64 +builder.test = 1 +rsync = tito@opensource.is:/var/www/sites/opensource.ok.is/repo/testing/fedora20/x86_64/ + + +[production-fc20-x86_64] +releaser = tito.release.YumRepoReleaser +builder = tito.builder.MockBuilder +builder.mock = fedora-20-x86_64 +rsync = tito@opensource.is:/var/www/sites/opensource.ok.is/repo/fedora20/x86_64/ + +# Fedora FC19 [test-fc19-x86_64] releaser = tito.release.YumRepoReleaser builder = tito.builder.MockBuilder From 3606e03170b798f39c9fe2fbbb2c2a23a29073dd Mon Sep 17 00:00:00 2001 From: Tomas Edwardsson Date: Mon, 30 Dec 2013 20:40:11 +0000 Subject: [PATCH 03/13] Automatic commit of package [nagios-okplugin-check_uptime] release [1.0.2-1]. --- check_uptime/nagios-okplugin-check_uptime.spec | 5 ++++- rel-eng/packages/nagios-okplugin-check_uptime | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/check_uptime/nagios-okplugin-check_uptime.spec b/check_uptime/nagios-okplugin-check_uptime.spec index c9a7ded..6d1df2c 100644 --- a/check_uptime/nagios-okplugin-check_uptime.spec +++ b/check_uptime/nagios-okplugin-check_uptime.spec @@ -2,7 +2,7 @@ Summary: A Nagios plugin to check uptime of a remote host via NRPE Name: nagios-okplugin-check_uptime -Version: 1.0.1 +Version: 1.0.2 Release: 1%{?dist} License: GPLv2+ Group: Applications/System @@ -41,6 +41,9 @@ rm -rf %{buildroot} /etc/nrpe.d/check_uptime.cfg %changelog +* Mon Dec 30 2013 Tomas Edwardsson 1.0.2-1 +- Updated tag for build with newer tito + * Mon Mar 12 2012 Pall Sigurdsson 1.0.1-1 - new package built with tito diff --git a/rel-eng/packages/nagios-okplugin-check_uptime b/rel-eng/packages/nagios-okplugin-check_uptime index 40e470f..c87ad69 100644 --- a/rel-eng/packages/nagios-okplugin-check_uptime +++ b/rel-eng/packages/nagios-okplugin-check_uptime @@ -1 +1 @@ -1.0.1-1 check_uptime/ +1.0.2-1 check_uptime/ From b375ae66ee3c7fb629c11d315961721c5df71cb2 Mon Sep 17 00:00:00 2001 From: Tomas Edwardsson Date: Mon, 20 Jan 2014 09:20:16 +0000 Subject: [PATCH 04/13] Added nagios-okplugin-common --- .../nagios-okplugin-common.spec | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 nagios-okplugin-common/nagios-okplugin-common.spec diff --git a/nagios-okplugin-common/nagios-okplugin-common.spec b/nagios-okplugin-common/nagios-okplugin-common.spec new file mode 100644 index 0000000..7c278b3 --- /dev/null +++ b/nagios-okplugin-common/nagios-okplugin-common.spec @@ -0,0 +1,41 @@ +%define debug_package %{nil} + +Summary: A common package for the okplugin suite +Name: nagios-okplugin-common +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: nrpe +BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n) +Packager: Tomas Edwardsson +BuildArch: noarch + +%description +Common utilities for okplugins + +%prep +%setup -q + +%build + + +%install +rm -rf %{buildroot} +mkdir -m 0770 -p %{buildroot}%{_sharedstatedir}/%{name} + +%clean +rm -rf %{buildroot} + +%post + +%files +%defattr(-,root,root,-) +%{_libdir}/nagios/plugins/* +%dir %attr(0770, nrpe, nrpe) %{_sharedstatedir}/%{name} + +%changelog +* Mon Jan 20 2014 Tomas Edwardsson 1.0-1 +- Initial build From 73b632a828dbdf4f6dbdca7744c2a6e5efe65258 Mon Sep 17 00:00:00 2001 From: Tomas Edwardsson Date: Mon, 20 Jan 2014 09:20:43 +0000 Subject: [PATCH 05/13] Automatic commit of package [nagios-okplugin-common] release [1.1-1]. --- nagios-okplugin-common/nagios-okplugin-common.spec | 5 ++++- rel-eng/packages/nagios-okplugin-common | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 rel-eng/packages/nagios-okplugin-common diff --git a/nagios-okplugin-common/nagios-okplugin-common.spec b/nagios-okplugin-common/nagios-okplugin-common.spec index 7c278b3..6eda1ea 100644 --- a/nagios-okplugin-common/nagios-okplugin-common.spec +++ b/nagios-okplugin-common/nagios-okplugin-common.spec @@ -2,7 +2,7 @@ Summary: A common package for the okplugin suite Name: nagios-okplugin-common -Version: 1.0 +Version: 1.1 Release: 1%{?dist} License: GPLv2+ Group: Applications/System @@ -37,5 +37,8 @@ rm -rf %{buildroot} %dir %attr(0770, nrpe, nrpe) %{_sharedstatedir}/%{name} %changelog +* Mon Jan 20 2014 Tomas Edwardsson 1.1-1 +- new package built with tito + * Mon Jan 20 2014 Tomas Edwardsson 1.0-1 - Initial build diff --git a/rel-eng/packages/nagios-okplugin-common b/rel-eng/packages/nagios-okplugin-common new file mode 100644 index 0000000..30c3495 --- /dev/null +++ b/rel-eng/packages/nagios-okplugin-common @@ -0,0 +1 @@ +1.1-1 nagios-okplugin-common/ From fcf90cae3e973693bbf5bcc541cdb20c94fcb012 Mon Sep 17 00:00:00 2001 From: Tomas Edwardsson Date: Mon, 20 Jan 2014 09:26:36 +0000 Subject: [PATCH 06/13] Removed invalid file reference --- nagios-okplugin-common/nagios-okplugin-common.spec | 1 - 1 file changed, 1 deletion(-) diff --git a/nagios-okplugin-common/nagios-okplugin-common.spec b/nagios-okplugin-common/nagios-okplugin-common.spec index 6eda1ea..8fc58a5 100644 --- a/nagios-okplugin-common/nagios-okplugin-common.spec +++ b/nagios-okplugin-common/nagios-okplugin-common.spec @@ -33,7 +33,6 @@ rm -rf %{buildroot} %files %defattr(-,root,root,-) -%{_libdir}/nagios/plugins/* %dir %attr(0770, nrpe, nrpe) %{_sharedstatedir}/%{name} %changelog From 971afc97f71b5e40c173a1db2f71208d8093aad0 Mon Sep 17 00:00:00 2001 From: Tomas Edwardsson Date: Mon, 20 Jan 2014 09:27:39 +0000 Subject: [PATCH 07/13] Automatic commit of package [nagios-okplugin-common] release [1.2-1]. --- nagios-okplugin-common/nagios-okplugin-common.spec | 5 ++++- rel-eng/packages/nagios-okplugin-common | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/nagios-okplugin-common/nagios-okplugin-common.spec b/nagios-okplugin-common/nagios-okplugin-common.spec index 8fc58a5..dab7b86 100644 --- a/nagios-okplugin-common/nagios-okplugin-common.spec +++ b/nagios-okplugin-common/nagios-okplugin-common.spec @@ -2,7 +2,7 @@ Summary: A common package for the okplugin suite Name: nagios-okplugin-common -Version: 1.1 +Version: 1.2 Release: 1%{?dist} License: GPLv2+ Group: Applications/System @@ -36,6 +36,9 @@ rm -rf %{buildroot} %dir %attr(0770, nrpe, nrpe) %{_sharedstatedir}/%{name} %changelog +* Mon Jan 20 2014 Tomas Edwardsson 1.2-1 +- + * Mon Jan 20 2014 Tomas Edwardsson 1.1-1 - new package built with tito diff --git a/rel-eng/packages/nagios-okplugin-common b/rel-eng/packages/nagios-okplugin-common index 30c3495..183bd24 100644 --- a/rel-eng/packages/nagios-okplugin-common +++ b/rel-eng/packages/nagios-okplugin-common @@ -1 +1 @@ -1.1-1 nagios-okplugin-common/ +1.2-1 nagios-okplugin-common/ From b10d555e528f06aac9543ff2c18e100404ac0c37 Mon Sep 17 00:00:00 2001 From: Tomas Edwardsson Date: Mon, 20 Jan 2014 10:14:52 +0000 Subject: [PATCH 08/13] Invalid directory fixed --- nagios-okplugin-common/nagios-okplugin-common.spec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nagios-okplugin-common/nagios-okplugin-common.spec b/nagios-okplugin-common/nagios-okplugin-common.spec index dab7b86..d27c21e 100644 --- a/nagios-okplugin-common/nagios-okplugin-common.spec +++ b/nagios-okplugin-common/nagios-okplugin-common.spec @@ -24,7 +24,7 @@ Common utilities for okplugins %install rm -rf %{buildroot} -mkdir -m 0770 -p %{buildroot}%{_sharedstatedir}/%{name} +mkdir -m 0770 -p %{buildroot}%{_sharedstatedir}/nagios-okplugin %clean rm -rf %{buildroot} @@ -33,7 +33,7 @@ rm -rf %{buildroot} %files %defattr(-,root,root,-) -%dir %attr(0770, nrpe, nrpe) %{_sharedstatedir}/%{name} +%dir %attr(0770, nrpe, nrpe) %{_sharedstatedir}/nagios-okplugin %changelog * Mon Jan 20 2014 Tomas Edwardsson 1.2-1 From ee4b10b291a9c6483c17c9d7eb1296c7e60b71cb Mon Sep 17 00:00:00 2001 From: Tomas Edwardsson Date: Mon, 20 Jan 2014 11:05:01 +0000 Subject: [PATCH 09/13] Added nagios-okplugin-check_cpu --- check_cpu.py/nagios-okplugin-check_cpu.spec | 58 +++++++++++++ check_cpu.py/nrpe.d/okplugin_check_cpu.cfg | 1 + check_cpu.py/okplugin_check_cpu | 91 +++++++++++++++++++++ 3 files changed, 150 insertions(+) create mode 100644 check_cpu.py/nagios-okplugin-check_cpu.spec create mode 100644 check_cpu.py/nrpe.d/okplugin_check_cpu.cfg create mode 100644 check_cpu.py/okplugin_check_cpu 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 From 5593391a3bd24c2363e40ab40230084ea98a9fa6 Mon Sep 17 00:00:00 2001 From: Tomas Edwardsson Date: Mon, 20 Jan 2014 11:05:13 +0000 Subject: [PATCH 10/13] Automatic commit of package [nagios-okplugin-check_cpu] release [1.1-1]. --- check_cpu.py/nagios-okplugin-check_cpu.spec | 5 ++++- rel-eng/packages/nagios-okplugin-check_cpu | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 rel-eng/packages/nagios-okplugin-check_cpu diff --git a/check_cpu.py/nagios-okplugin-check_cpu.spec b/check_cpu.py/nagios-okplugin-check_cpu.spec index 2f10f91..faaffc1 100644 --- a/check_cpu.py/nagios-okplugin-check_cpu.spec +++ b/check_cpu.py/nagios-okplugin-check_cpu.spec @@ -2,7 +2,7 @@ Summary: A Nagios plugin to check CPU on Linux servers Name: nagios-okplugin-check_cpu -Version: 1.0 +Version: 1.1 Release: 1%{?dist} License: GPLv2+ Group: Applications/System @@ -44,6 +44,9 @@ rm -rf %{buildroot} %changelog +* Mon Jan 20 2014 Tomas Edwardsson 1.1-1 +- new package built with tito + * Thu Aug 23 2012 Pall Sigurdsson 1.0-1 - Version number bumped - Updates buildarch to noarch (tommi@tommi.org) diff --git a/rel-eng/packages/nagios-okplugin-check_cpu b/rel-eng/packages/nagios-okplugin-check_cpu new file mode 100644 index 0000000..29beaae --- /dev/null +++ b/rel-eng/packages/nagios-okplugin-check_cpu @@ -0,0 +1 @@ +1.1-1 check_cpu.py/ From 735b2795d8e31fec4e5bcb87a9d7aaf4bd77a0cf Mon Sep 17 00:00:00 2001 From: Tomas Edwardsson Date: Mon, 20 Jan 2014 11:05:52 +0000 Subject: [PATCH 11/13] Removed unneeded directory for state --- check_cpu.py/nagios-okplugin-check_cpu.spec | 2 -- 1 file changed, 2 deletions(-) diff --git a/check_cpu.py/nagios-okplugin-check_cpu.spec b/check_cpu.py/nagios-okplugin-check_cpu.spec index faaffc1..728315f 100644 --- a/check_cpu.py/nagios-okplugin-check_cpu.spec +++ b/check_cpu.py/nagios-okplugin-check_cpu.spec @@ -24,7 +24,6 @@ Check cpu states on line machines %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 @@ -37,7 +36,6 @@ rm -rf %{buildroot} %files %defattr(-,root,root,-) -%dir %attr(0770, nrpe, nrpe) %{_sharedstatedir}/nagios-okplugin/%{name} %{_libdir}/nagios/plugins/* %{_sysconfdir}/nrpe.d/* From 5d0df0bfe9c669bb2ef85d48552fd60dd0f80956 Mon Sep 17 00:00:00 2001 From: Tomas Edwardsson Date: Mon, 20 Jan 2014 11:41:11 +0000 Subject: [PATCH 12/13] Check would fail on first run --- check_cpu.py/okplugin_check_cpu | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/check_cpu.py/okplugin_check_cpu b/check_cpu.py/okplugin_check_cpu index 3230c92..1ab9da9 100644 --- a/check_cpu.py/okplugin_check_cpu +++ b/check_cpu.py/okplugin_check_cpu @@ -34,7 +34,10 @@ def main(): stat = get_stat() # Find shortest stat - stat_length = len(old_stat) > len(stat) and len(old_stat) or len(stat) + if old_stat: + stat_length = len(old_stat) > len(stat) and len(old_stat) or len(stat) + else: + stat_length = len(stat) if old_stat: diff = [] From 067f58f52e725d65fe0ee9db4526f73046a45cb4 Mon Sep 17 00:00:00 2001 From: Tomas Edwardsson Date: Mon, 20 Jan 2014 17:50:28 +0000 Subject: [PATCH 13/13] Added returning of a 5 second sample in perfdata --- check_cpu.py/okplugin_check_cpu | 76 ++++++++++++++++++++++++--------- 1 file changed, 57 insertions(+), 19 deletions(-) diff --git a/check_cpu.py/okplugin_check_cpu b/check_cpu.py/okplugin_check_cpu index 1ab9da9..5733324 100644 --- a/check_cpu.py/okplugin_check_cpu +++ b/check_cpu.py/okplugin_check_cpu @@ -1,6 +1,6 @@ #!/usr/bin/python # -# Copyright 2014, Tomas Edwardsson +# 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 @@ -16,9 +16,13 @@ # along with this program. If not, see . """ Gathers information on cpu usage on a linux machine + +It returns performance data for both the last 5 seconds and from last run. """ from pynag.Plugins import PluginHelper, ok, unknown +import os +import time def main(): @@ -30,36 +34,68 @@ def main(): pyplug = PluginHelper() + # Get the statistics from last run old_stat = get_saved_stat() + + # Get current state stat = get_stat() - # Find shortest stat - if old_stat: - stat_length = len(old_stat) > len(stat) and len(old_stat) or len(stat) - else: - stat_length = 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="%") + # Resample + time.sleep(5) + stat2 = get_stat() try: save_stat(stat) except IOError, err: pyplug.exit(unknown, "Unable to save cpu statistics: %s" % err) + + # Calculate averages for the last few seconds + current_averages = diff_stats(stat2, stat) + for pos in range(len(current_averages)): + pyplug.add_metric(label=stat_fields[pos+1], + value=current_averages[pos], + uom="%") + + # Calculate averages from last run + if old_stat: + last_run_avg = diff_stats(old_stat, stat) + for pos in range(len(current_averages)): + pyplug.add_metric(label=stat_fields[pos+1]+"_avg", + value=last_run_avg[pos], + uom="%") + pyplug.add_status(ok) - pyplug.add_summary("CPU load %.2f%%" % (100.0 - averages[3])) + pyplug.add_summary("CPU load %.2f%%" % (100.0 - current_averages[3])) pyplug.exit() +def diff_stats(older, newer): + """Calculates the percentage between two stat arrays""" + if older: + diff = [] + for pos in range(len(newer)): + diff.append(newer[pos] - older[pos]) + averages = calc_avg(diff) + else: + averages = calc_avg(newer) + + return averages + + +def remove_stale_state(): + """Removes the last state file if we have rebooted""" + + try: + state_mtime = os.stat("/var/lib/nagios-okplugin/check_cpu").st_mtime + boot_time = os.stat("/proc/1").st_mtime + if state_mtime < boot_time: + print "unlinking" + os.unlink("/var/lib/nagios-okplugin/check_cpu") + except OSError: + return None + + return state_mtime + def calc_avg(stat): """Calculates the average of each int in array""" stat_sum = sum(stat) @@ -75,6 +111,8 @@ def get_stat(): def get_saved_stat(): """Fetches information about the last run to be able to do averages""" + + remove_stale_state() try: stat_fh = open("/var/lib/nagios-okplugin/check_cpu") except IOError: