-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.py
executable file
·75 lines (62 loc) · 2.69 KB
/
functions.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
69
70
71
72
73
74
75
#!/usr/bin/env python3
import os
import requests
import socket
from configread import configread
from pyzabbix import ZabbixSender
from socket import timeout
from sys import stderr
# set project name as current directory name
project = os.path.abspath(__file__).split('/')[-2]
# get config file name
conf_file = (f'/etc/zabbix/externalscripts/{project}/{project}.conf')
# read network device parameters from config and save it to dict
nd_parameters = configread(conf_file, 'NetworkDevice',
'slack_hook', 'zabbix_server')
# get variables for error notifications
slack_hook = nd_parameters['slack_hook']
zabbix_server = nd_parameters['zabbix_server']
hostname = socket.gethostname()
def slack_post(software, message, icon_emoji=':snake:'):
""" post alarms to slack channel """
if slack_hook:
requests.post(slack_hook, json={'username': hostname,
'icon_emoji': icon_emoji,
'text': f'{project}_error: exception in {software}: {message}'})
def zabbix_send(data, printing, software):
""" send packet of data to zabbix """
try:
zabbix_send_status = ZabbixSender(zabbix_server).send(data)
if printing:
print(
f'Status of sending data to zabbix:\n{zabbix_send_status}')
except timeout as error:
print(
(f'{project}_error: exception in {software}: Zabbix trapper socket timeout on {zabbix_server}: {error}. '
f'Check health status of zabbix server.'),
file=stderr)
slack_post(
software,
(f'Zabbix trapper socket timeout on {zabbix_server}: {error}. '
f'Check health status of zabbix server.'))
exit(1)
except ConnectionRefusedError as error:
print(
(f'{project}_error: exception in {software}: Zabbix trapper refused connection on {zabbix_server}: {error}. '
f'Check running of zabbix trappers and firewall trafic permissions.'),
file=stderr)
slack_post(
software,
(f'Zabbix trapper refused connection on {zabbix_server}: {error}. '
f'Check running of zabbix trappers and firewall trafic permissions.'))
exit(1)
except ConnectionResetError as error:
print(
(f'{project}_error: exception in {software}: Zabbix trapper reset connection on {zabbix_server}: {error}. '
f'Check running of zabbix trappers.'),
file=stderr)
slack_post(
software,
(f'Zabbix trapper reset connection on {zabbix_server}: {error}. '
f'Check running of zabbix trappers.'))
exit(1)