-
Notifications
You must be signed in to change notification settings - Fork 7
/
librenms.py
68 lines (57 loc) · 1.94 KB
/
librenms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#! /usr/bin/env python3
# -*- coding: utf-8; py-indent-offset: 4 -*-
#
# Author: Linuxfabrik GmbH, Zurich, Switzerland
# Contact: info (at) linuxfabrik (dot) ch
# https://www.linuxfabrik.ch/
# License: The Unlicense, see LICENSE file.
# https://github.com/Linuxfabrik/monitoring-plugins/blob/main/CONTRIBUTING.rst
"""This library collects some LibreNMS related functions that are
needed by LibreNMS check plugins."""
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
__version__ = '2024090301'
from .globals import STATE_CRIT, STATE_OK, STATE_UNKNOWN, STATE_WARN
from . import base # pylint: disable=C0413
from . import txt # pylint: disable=C0413
from . import url # pylint: disable=C0413
def get_data(args, uri=''):
if args.URL.endswith('/'):
args.URL = args.URL[:-1]
if not uri.startswith('/'):
uri = '/' + uri
uri = '{}{}'.format(args.URL, uri)
header = {'X-Auth-Token': args.TOKEN}
result = base.coe(url.fetch_json(
uri,
header=header,
insecure=args.INSECURE,
no_proxy=args.NO_PROXY,
timeout=args.TIMEOUT,
))
if result['status'].lower() != 'ok':
base.oao(
'Error fetching data: "{}"'.format(result),
STATE_UNKNOWN,
always_ok=args.ALWAYS_OK,
)
return result
def get_prop(obj, prop, mytype='str'):
"""Get a property of a dict, for example device['uptime'], and handle None-values."""
if mytype == 'str':
if prop in obj:
if obj[prop] is not None:
return txt.to_text(obj[prop])
return ''
if prop in obj:
if obj[prop] is not None:
return obj[prop]
return None
def get_state(librestate, severity='crit'):
"""Translate LibreNMS' state to the Nagios world.
librestate: 0 = ok, 1 = alert, 2 = ack
"""
if librestate == 1:
if severity == 'crit':
return STATE_CRIT
return STATE_WARN
return STATE_OK