Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions shinken/misc/perfdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,27 @@ def __init__(self, s):
self.critical = guess_int_or_float(r.group(5))
self.min = guess_int_or_float(r.group(6))
self.max = guess_int_or_float(r.group(7))
self.min_specified = self.min is not None
self.max_specified = self.max is not None
# print 'Name', self.name
# print "Value", self.value
# print "Res", r
# print r.groups()
if self.uom == '%':
self.min = 0
self.max = 100
self.min = self.min if self.min_specified is not None else 0
self.max = self.max if self.max_specified is not None else 100

def __str__(self):
s = "%s=%s%s" % (self.name, self.value, self.uom)
if self.warning:
s = s + ";%s" % (self.warning)
if self.critical:
s = s + ";%s" % (self.critical)
return s
min = self.min if self.min_specified else None
max = self.max if self.max_specified else None
prefix = "min=%s, max=%s" % (min, max)
if self.uom == '%':
min = None if (self.min == 0) and not self.min_specified else min
max = None if (self.max == 100) and not self.max_specified else max
components = ["%s=%s%s" % (self.name, self.value, self.uom), self.warning, self.critical, min, max]
while components[-1] is None:
components.pop()
return ";".join(map(lambda v: "" if v is None else str(v), components))


class PerfDatas:
Expand All @@ -101,3 +107,4 @@ def __getitem__(self, key):

def __contains__(self, key):
return key in self.metrics

82 changes: 82 additions & 0 deletions test/test_string_perfdata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2009-2014:
# Gabes Jean, naparuba@gmail.com
# Gerhard Lausser, Gerhard.Lausser@consol.de
#
# This file is part of Shinken.
#
# Shinken is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Shinken 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Shinken. If not, see <http://www.gnu.org/licenses/>.

#
# This file is used to test reading and processing of config files
#

from shinken_test import *
from shinken.misc.perfdata import Metric, PerfDatas


class TestStringPerfdata(ShinkenTest):
# Uncomment this is you want to use a specific configuration
# for your test
#def setUp(self):
# self.setup_with_file('etc/shinken_parse_perfdata.cfg')

def test_string_all_four(self):
self.assertEqual('ramused=1009MB;1;2;3;4', str(Metric('ramused=1009MB;1;2;3;4')))

def test_string_drop_empty_from_end(self):
self.assertEqual('ramused=1009MB', str(Metric('ramused=1009MB')))
self.assertEqual('ramused=1009MB;1', str(Metric('ramused=1009MB;1')))
self.assertEqual('ramused=1009MB;1;2', str(Metric('ramused=1009MB;1;2')))
self.assertEqual('ramused=1009MB;1;2;3', str(Metric('ramused=1009MB;1;2;3')))
self.assertEqual('ramused=1009MB;1;2;3;4', str(Metric('ramused=1009MB;1;2;3;4')))

def test_string_empty_for_None(self):
self.assertEqual('ramused=1009MB', str(Metric('ramused=1009MB;')))
self.assertEqual('ramused=1009MB', str(Metric('ramused=1009MB;;')))
self.assertEqual('ramused=1009MB', str(Metric('ramused=1009MB;;;')))
self.assertEqual('ramused=1009MB', str(Metric('ramused=1009MB;;;;')))

self.assertEqual('ramused=1009MB;;2', str(Metric('ramused=1009MB;;2')))
self.assertEqual('ramused=1009MB;;;3', str(Metric('ramused=1009MB;;;3')))
self.assertEqual('ramused=1009MB;;;;4', str(Metric('ramused=1009MB;;;;4')))

self.assertEqual('ramused=1009MB;;2;;4', str(Metric('ramused=1009MB;;2;;4')))

def test_string_zero_preserved(self):
self.assertEqual('ramused=1009MB;0', str(Metric('ramused=1009MB;0')))
self.assertEqual('ramused=1009MB;;0', str(Metric('ramused=1009MB;;0')))
self.assertEqual('ramused=1009MB;;;0', str(Metric('ramused=1009MB;;;0')))
self.assertEqual('ramused=1009MB;;;;0', str(Metric('ramused=1009MB;;;;0')))

def test_string_percent_minmaxdefault_0_100(self):
# If not specified, defaults of 0 and 100 for min/max should not come back
self.assertEqual('utilization=80%;90;95', str(Metric('utilization=80%;90;95')))
self.assertEqual('utilization=80%;90;95;;', str(Metric('utilization=80%;90;95')) + ";;")

def test_string_percent_minmax_echo(self):
# Defined values of min max should come back always, even if defaults
self.assertEqual('utilization=80%;50;75;0;100', str(Metric('utilization=80%;50;75;0;100')))
self.assertEqual('utilization=80%;50;75;0', str(Metric('utilization=80%;50;75;0')))
self.assertEqual('utilization=80%;50;75;;100', str(Metric('utilization=80%;50;75;;100')))

# Same tests with non-default values
self.assertEqual('utilization=80%;50;75;85;95', str(Metric('utilization=80%;50;75;85;95')))
self.assertEqual('utilization=80%;50;75;85', str(Metric('utilization=80%;50;75;85')))
self.assertEqual('utilization=80%;50;75;;95', str(Metric('utilization=80%;50;75;;95')))

if __name__ == '__main__':
unittest.main()