Misc utility classes and helper functions for pynag
This module contains misc classes and conveninence functions that are used throughout the pynag library.
Bases: object
Parse a list of nagios attributes (e. contact_groups) into a parsable format.
This makes it handy to mangle with nagios attribute values that are in a comma seperated format.
Typical comma-seperated format in nagios configuration files looks something like this:
contact_groups +group1,group2,group3
Example:
>>> i = AttributeList('+group1,group2,group3')
>>> i.operator
+
>>> i.fields
['group1', 'group2', 'group3']
# if your data is already in a list format you can use it directly:
>>> i = AttributeList(['group1', 'group2', 'group3'])
>>> i.fields
['group1', 'group2', 'group3']
# white spaces will be stripped from all fields
>>> i = AttributeList('+group1, group2')
>>> i
+group1,group2
>>> i.fields
['group1', 'group2']
Same as list.append():
>>> i = AttributeList('group1,group2,group3')
>>> i.append('group5')
>>> i.fields
['group1', 'group2', 'group3', 'group5']
Same as list.count()
>>> i = AttributeList('group1,group2,group3')
>>> i.count('group3')
1
Same as list.extend()
>>> i = AttributeList('group1,group2,group3')
>>> i.extend(['group4', 'group5'])
>>> i.fields
['group1', 'group2', 'group3', 'group4', 'group5']
Same as list.index()
>>> i = AttributeList('group1,group2,group3')
>>> i.index('group2')
1
>>> i.index('group3', 2, 5)
2
Same as list.insert()
>>> i = AttributeList('group1,group2,group3')
>>> i.insert(1, 'group4')
>>> i.fields
['group1', 'group4', 'group2', 'group3']
Same as list.remove()
>>> i = AttributeList('group1,group2,group3')
>>> i.remove('group3')
>>> i.fields
['group1', 'group2']
Same as list.reverse()
>>> i = AttributeList('group1,group2,group3')
>>> i.reverse()
>>> i.fields
['group3', 'group2', 'group1']
Same as list.sort()
>>> i = AttributeList('group3,group1,group2')
>>> i.sort()
>>> print(i.fields)
['group1', 'group2', 'group3']
Bases: object
Run git add on filename
Commit files with “git commit”
Arguments:
message (str): Message used for the git commit
- filelist (list of strings): List of filenames to commit (if None,
- then commit all files in the repo)
- author (str): Author to use for git commit. If any is specified,
- overwrite self.author_name and self.author_email
Returns:
stdout from the “git commit” shell command.
Returns diff (as outputted by “git diff”) for filename or commit id.
If commit_id_or_filename is not specified. show diff against all uncommited files.
Returns a list of files that are have unstaged changes
Returns a list of all commit ids from git log
Initilizes a new git repo (i.e. run “git init”)
Returns True if filename needs to be committed to git
Arguments:
filename (str): file to check
Returns True if all files in git repo are fully commited
bool. Git repo is up-to-date
True – All files are commited
False – At least one file is not commited
Returns a log of previous commits. Log is is a list of dict objects.
Any arguments provided will be passed directly to pynag.Utils.grep() to filter the results.
Commits object_definition.get_filename() if it has any changes. This function is called by pynag.Model.EventHandlers before calling pynag.Utils.GitRepo.save()
Arguments:
object_definition (pynag.Model.ObjectDefinition): object to commit changes
message (str): git commit message as specified in git commit -m
Revert some existing commits works like “git revert”
Commits object_definition.get_filename() if it has any changes. This function is called by pynag.Model.EventHandlers
Arguments:
object_definition (pynag.Model.ObjectDefinition): object to commit changes
message (str): git commit message as specified in git commit -m
Returns output from “git show” for a specified commit_id
This method is called whenever pynag.Model.EventHandlers is called.
Arguments
object_definition (pynag.Model.ObjectDefinition): Object to write to file.
message (str): git commit message as specified in git commit -m
Bases: object
Data Structure for a nagios perfdata string with multiple perfdata metric
Example string:
>>> perf = PerfData("load1=10 load2=10 load3=20 'label with spaces'=5")
>>> perf.metrics
['load1'=10;;;;, 'load2'=10;;;;, 'load3'=20;;;;, 'label with spaces'=5;;;;]
>>> for i in perf.metrics: print("%s %s" % (i.label, i.value))
load1 10
load2 10
load3 20
label with spaces 5
Add a new perfdatametric to existing list of metrics.
Arguments:
perfdatastring (str): Complete perfdata string
label (str): Label section of the perfdata string
value (str): Value section of the perfdata string
warn (str): WARNING threshold
crit (str): CRITICAL threshold
min (str): Minimal value of control
max (str): Maximal value of control
uom (str): Measure unit (octets, bits/s, volts, ...)
Example:
>>> s = PerfData()
>>> s.add_perfdatametric("a=1")
>>> s.add_perfdatametric(label="utilization",value="10",uom="%")
Get one specific perfdatametric
Example:
>>> s = PerfData("cpu=90% memory=50% disk_usage=20%")
>>> my_metric = s.get_perfdatametric('cpu')
>>> my_metric.label, my_metric.value
('cpu', '90')
Returns True if the every metric in the string is valid
Example usage:
>>> PerfData("load1=10 load2=10 load3=20").is_valid()
True
>>> PerfData("10b").is_valid()
False
>>> PerfData("load1=").is_valid()
False
>>> PerfData("load1=10 10").is_valid()
False
Convert all thresholds in new_threshold_syntax to the standard one
Bases: object
Data structure for one single Nagios Perfdata Metric
Attributes:
perfdatastring (str): Complete perfdata string
label (str): Label section of the perfdata string
value (str): Value section of the perfdata string
warn (str): WARNING threshold
crit (str): CRITICAL threshold
min (str): Minimal value of control
max (str): Maximal value of control
uom (str): Measure unit (octets, bits/s, volts, ...)
str CRITICAL threshold
Returns a dictionary which contains this class’ attributes.
Returned dict example:
{
'label': self.label,
'value': self.value,
'uom': self.uom,
'warn': self.warn,
'crit': self.crit,
'min': self.min,
'max': self.max,
}
Return nagios-style exit code (int 0-3) by comparing
Example:
self.value with self.warn and self.crit
>>> PerfDataMetric("label1=10;20;30").get_status()
0
>>> PerfDataMetric("label2=25;20;30").get_status()
1
>>> PerfDataMetric("label3=35;20;30").get_status()
2
Invalid metrics always return unknown
>>> PerfDataMetric("label3=35;invalid_metric").get_status()
3
Returns True if all Performance data is valid. Otherwise False
Example Usage:
>>> PerfDataMetric("load1=2").is_valid()
True
>>> PerfDataMetric("load1").is_valid()
False
>>> PerfDataMetric('').is_valid()
False
>>> PerfDataMetric('invalid_value=invalid').is_valid()
False
>>> PerfDataMetric('invalid_min=0;0;0;min;0').is_valid()
False
>>> PerfDataMetric('invalid_min=0;0;0;0;max').is_valid()
False
>>> PerfDataMetric('label with spaces=0').is_valid()
False
>>> PerfDataMetric("'label with spaces=0'").is_valid()
False
str. Label section of the perfdata string
str. Maximal value of control
str. Minimal value of control
Convert threshold from new threshold syntax to current one, for backwards compatibility
Example:
get value=”10M” and return (10,”M”)
>>> p = PerfDataMetric()
>>> p.split_value_and_uom( "10" )
('10', '')
>>> p.split_value_and_uom( "10c" )
('10', 'c')
>>> p.split_value_and_uom( "10B" )
('10', 'B')
>>> p.split_value_and_uom( "10MB" )
('10', 'MB')
>>> p.split_value_and_uom( "10KB" )
('10', 'KB')
>>> p.split_value_and_uom( "10TB" )
('10', 'TB')
>>> p.split_value_and_uom( "10%" )
('10', '%')
>>> p.split_value_and_uom( "10s" )
('10', 's')
>>> p.split_value_and_uom( "10us" )
('10', 'us')
>>> p.split_value_and_uom( "10ms" )
('10', 'ms')
str. Measure unit (octets, bits/s, volts, ...)
str. Value section of the perfdata string
str. WARNING threshold
This class parses a typical stdout from a nagios plugin
It splits the output into the following fields:
Attributes:
summary (str): Summary returned by the plugin check
long_output (str)
perfdata (str): Data returned by the plugin as a string
parsed_perfdata: perfdata parsed and split
Example Usage:
>>> p = PluginOutput("Everything is ok | load1=15 load2=10")
>>> p.summary
'Everything is ok '
>>> p.long_output
''
>>> p.perfdata
'load1=15 load2=10'
>>> p.parsed_perfdata.metrics
['load1'=15;;;;, 'load2'=10;;;;]
Perfdata parsed and split
str. Data returned by the plugin as a string
str. Summary returned by the plugin check
Bases: exceptions.Exception
The default pynag exception.
Exceptions raised within the pynag library should aim to inherit this one.
Bases: dict
This is an alternative implementation of collections.defaultdict.
Used as a fallback if using python 2.4 or older.
Usage:
try:
from collections import defaultdict
except ImportError:
from pynag.Utils import defaultdict
Returns all the elements from array that match the keywords in **kwargs
See documentation for pynag.Model.ObjectDefinition.objects.filter() for example how to use this.
Arguments:
objects (list of dict): list to be searched
kwargs (str): Any search argument provided will be checked against every dict
Examples:
array = [
{'host_name': 'examplehost', 'state':0},
{'host_name': 'example2', 'state':1},
]
grep_dict(array, state=0)
# should return [{'host_name': 'examplehost', 'state':0},]
Converts from pynag style grep syntax to livestatus filter syntax.
Example:
>>> grep_to_livestatus(host_name='test')
['Filter: host_name = test']
>>> grep_to_livestatus(service_description__contains='serv')
['Filter: service_description ~ serv']
>>> grep_to_livestatus(service_description__isnot='serv')
['Filter: service_description != serv']
>>> grep_to_livestatus(service_description__contains=['serv','check'])
['Filter: service_description ~ serv']
>>> grep_to_livestatus(service_description__contains='foo', contacts__has_field='admin')
['Filter: contacts >= admin', 'Filter: service_description ~ foo']
>>> grep_to_livestatus(service_description__has_field='foo')
['Filter: service_description >= foo']
>>> grep_to_livestatus(service_description__startswith='foo')
['Filter: service_description ~ ^foo']
>>> grep_to_livestatus(service_description__endswith='foo')
['Filter: service_description ~ foo$']
Take threshold string as and normalize it to the format supported by plugin development team
The input (usually a string in the form of ‘the new threshold syntax’) is a string in the form of x..y
The output will be a compatible string in the older nagios plugin format @x:y
Examples:
>>> reconsile_threshold("0..5")
'@0:5'
>>> reconsile_threshold("inf..5")
'5:'
>>> reconsile_threshold("5..inf")
'~:5'
>>> reconsile_threshold("inf..inf")
'@~:'
>>> reconsile_threshold("^0..5")
'0:5'
>>> reconsile_threshold("10..20")
'@10:20'
>>> reconsile_threshold("10..inf")
'~:10'
Run command from the shell prompt. Wrapper around subprocess.
Send data via send_nsca for passive service checks
Arguments:
code (int): Return code of plugin.
message (str): Message to pass back.
nscahost (str): Hostname or IP address of NSCA server.
hostname (str): Hostname the check results apply to.
service (str): Service the check results apply to.
- nscabin (str): Location of send_nsca binary. If none specified whatever
- is in the path will be used.
nscaconf (str): Location of the NSCA configuration to use if any.
Returns:
[result,stdout,stderr] of the command being run
Synchronization decorator
Use this to make a multi-threaded method synchronized and thread-safe.
Use the decorator like so:
@pynag.Utils.synchronized(pynag.Utils.rlock)