Host¶
This module defined the naghelp Host object.
The Host object will store all informations about the equipment to monitor. It could be for exemple :
- The hostname
- Host IP address
- The user login
- The user password
- The snmp community
- …
One can add some additional data during the plugin execution, they will be persistent accross
all plugin executions (plugin objects call save_data()
and
load_data()
methods). This is useful for :
- Counters
- Gauges
- Timestamps
- Flags
- …
Informations come from 3 sources in this order :
- From a database ( text file, sqlite, mysql … )
- From environment variables
- From command line
Informations from command line have priority over environment vars which have priority over those from database.
-
class
naghelp.
Host
(plugin)¶ Contains equipment informations
Host object is a dict with some additional methods.
Parameters: plugin ( Plugin
) – The plugin object that is used to monitor the equipmentInformations can be accessed and modified by 2 ways :
- By attribute
- By Index
To save and load custom data, one could do a
save_data()
andload_data()
but this is automatically done by the plugin itself (seenaghelp.ActivePlugin.run()
)Examples :
>>> os.environ['NAGIOS_HOSTNAME']='host_to_be_monitored' >>> plugin = ActivePlugin() >>> host = Host(plugin) >>> print host.name host_to_be_monitored >>> host.my_custom_data = 'last check time' >>> print host.my_custom_data last check time >>> print host['my_custom_data'] last check time >>> host.save_data() >>> host._get_persistent_filename() '/tmp/naghelp/host_to_be_monitored_persistent_data.json' >>> print open(host._get_persistent_filename()).read() { "name": "host_to_be_monitored", "my_custom_data": "last check time" }
>>> os.environ['NAGIOS_HOSTNAME']='host_to_be_monitored' >>> plugin = ActivePlugin() >>> host = Host(plugin) >>> print host.my_custom_data >>> host.load_data() >>> print host.my_custom_data last check time
-
_get_env_to_param
()¶ Returns a dict for the environment variable to extract
The keys are the environment variables to extract, the values are the attribute name to use for the Host object.
Only a few environment variables are automatically extracted, they are renamed when saved into Host object :
Environment Variables Stored as NAGIOS_HOSTNAME name NAGIOS_HOSTALIAS alias NAGIOS_HOSTADDRESS ip NAGIOS_HOSTGROUPNAMES groups NAGIOS_HOSTGROUPNAME group
-
_get_params_from_db
(hostname)¶ Get host informations from database
Getting informations from a database is optionnal. If needed, it is the developer responsibility to subclass
Host
class and redefine the method_get_params_from_db(hostname)
that returns a dict with informations abouthostname
. In this method, the developer must also load persistent data that may come from a same cache file as the database. The default behaviour for this method is only to load persistent data from a database flat file (.json).Parameters: hostname (str) – The name of the equipment as declared in Nagios configuration files. Returns: A dictionary that contains equipment informations AND persistent data merged. Return type: dict Example
Here is an example of
_get_params_from_db()
override where informations about a monitored host are stored in a json file located at DB_JSON_FILEclass MonitoredHost(Host): def _get_params_from_db(self,hostname): # The first step MUST be to read the persistent data file ( = cache file ) params = self._plugin.load_data(self._get_persistent_filename()) or DictExt() # Check whether the database file has changed db_file_modif_time = int(os.path.getmtime(DB_JSON_FILE)) if db_file_modif_time == params['db_file_modif_time']: # if not, return the cached data return params # If database file has changed : db = json.load(open(DB_JSON_FILE)) # find hostname in db : for h in db.monitored_hosts: if h['datas']['hostname'] == hostname: # merge the new data into the persistent data dict (will be cached) params.update(h.datas) params['db_file_modif_time'] = db_file_modif_time params['name'] = params.get('hostname','noname') return params return params
Note
You can do about the same for SQlite or MySQL, do not forget to load persistent data file as a first step and merge data from database after. Like the above example, you can use the persistent data json file as a cache for your database. By this way, persistent data AND database data are saved in the same file within a single operation. The dictionary returned by this method will be saved automatically by the
naghelp.ActivePlugin.run()
method as persistent data.
-
_get_persistent_filename
()¶ Get the full path for the persisten .json file
It uses
persistent_filename_pattern
to get the file pattern, and apply the hostname to build the full path.
-
debug
()¶ Log Host informations for debug
Note
To see debug on python console, call
naghelp.activate_debug()
-
get
(k[, d]) → D[k] if k in D, else d. d defaults to None.¶
-
load_data
()¶ load data to the
Host
objectThat is from database and/or persistent file then from environment variables and then from command line.
-
persistent_filename_pattern
= '/tmp/naghelp/%s_persistent_data.json'¶ Default persistent .json file path pattern (note the %s that will be replaced by the hostname)
-
save_data
()¶ Save data to a persistent place
It actually saves the whole dict into a .json file. This is automatically called by the :meth:naghelp.ActivePlugin.run method.
-
to_list
(lst, defvalue='-')¶ Formats a list of strings with Host informations
It works like
to_str()
except that the input is a list of strings : This useful when a text has been splitted into lines.Parameters: Returns: The list with formatted string
Return type: list
Examples
>>> os.environ['NAGIOS_HOSTNAME']='host_to_be_monitored' >>> os.environ['NAGIOS_HOSTADDRESS']='192.168.0.33' >>> plugin = ActivePlugin() >>> host = Host(plugin) >>> host.load_data() >>> lst = ['{name} as got IP={ip} and custom data "{my_custom_data}"', ... 'Not available data are replaced by a dash: {other_data}'] >>> print host.to_list(lst) [u'host_to_be_monitored as got IP=192.168.0.33 and custom data "last check time"', 'Not available data are replaced by a dash: -']
Note
As you noticed,
NAGIOS_HOSTNAME
environment variable is stored asname
in Host object andNAGIOS_HOSTADDRESS
asip
.my_custom_data
is a persistent data that has been automatically loaded because set into a previous example.
-
to_str
(str, defvalue='-')¶ Formats a string with Host informations
Not available data are replaced by a dash
Parameters: Returns: the formatted string
Return type: Examples
>>> os.environ['NAGIOS_HOSTNAME']='host_to_be_monitored' >>> os.environ['NAGIOS_HOSTADDRESS']='192.168.0.33' >>> plugin = ActivePlugin() >>> host = Host(plugin) >>> host.load_data() >>> print host.to_str('{name} as got IP={ip} and custom data "{my_custom_data}"') host_to_be_monitored as got IP=192.168.0.33 and custom data "last check time" >>> print host.to_str('Not available data are replaced by a dash: {other_data}') Not available data are replaced by a dash: - >>> print host.to_str('Or by whatever you want: {other_data}','N/A') Or by whatever you want: N/A
Note
As you noticed,
NAGIOS_HOSTNAME
environment variable is stored asname
in Host object andNAGIOS_HOSTADDRESS
asip
.my_custom_data
is a persistent data that has been automatically loaded because set into a previous example.