Initial commit
This commit is contained in:
parent
b9c0f3256a
commit
891cfbfb60
18
LICENSE
Normal file
18
LICENSE
Normal file
|
@ -0,0 +1,18 @@
|
|||
Copyright (c) 2015-2016 deflax.net
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgement in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
|
6
start.sh
Executable file
6
start.sh
Executable file
|
@ -0,0 +1,6 @@
|
|||
#!/bin/bash
|
||||
|
||||
until /home/steinvord/steinvord/steinvord.py; do
|
||||
echo "Steinvord crashed with exit code $?. Respawning.." >&2
|
||||
sleep 1
|
||||
done
|
143
steinvord.py
Executable file
143
steinvord.py
Executable file
|
@ -0,0 +1,143 @@
|
|||
#!/usr/bin/python3.2
|
||||
|
||||
###
|
||||
### steinvord.py
|
||||
###
|
||||
|
||||
|
||||
## SETUP
|
||||
|
||||
z_url = 'http://fqdn/zabbix/'
|
||||
z_user = 'USER'
|
||||
z_pass = 'PASS'
|
||||
|
||||
server = 'irc.example.com'
|
||||
channel = '#example'
|
||||
nick = 'steinvord'
|
||||
botnick = 'Steinvord'
|
||||
botuser = 'steinvord'
|
||||
|
||||
encoding = 'utf-8'
|
||||
calibrate = 0
|
||||
|
||||
|
||||
### begin
|
||||
import sys, socket, time, re, datetime
|
||||
from pyzabbix import ZabbixAPI
|
||||
from difflib import Differ
|
||||
|
||||
trigprevlist = []
|
||||
triglist = []
|
||||
counter = 0
|
||||
|
||||
try:
|
||||
zapi = ZabbixAPI(z_url)
|
||||
zapi.session.timeout = 5
|
||||
zapi.login(z_user, z_pass)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
exit(3)
|
||||
print ("Connected to Zabbix API Version %s" % zapi.api_version())
|
||||
|
||||
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #defines the socket
|
||||
print("Establishing connection to [%s]" % (server))
|
||||
irc.connect((server, 6667)) #connects to the server
|
||||
irc.setblocking(False)
|
||||
|
||||
sock_auth = "USER "+ botuser +" "+ botuser +" "+ botuser +" :This is the Steinvord bot!\n"
|
||||
sock_nick = "NICK "+ botnick +"\n"
|
||||
sock_nicksrv = "PRIVMSG nickserv :iNOOPE\r\n"
|
||||
sock_join = "JOIN "+ channel +"\n"
|
||||
irc.send(sock_auth.encode(encoding))
|
||||
irc.send(sock_nick.encode(encoding))
|
||||
irc.send(sock_nicksrv.encode(encoding))
|
||||
irc.send(sock_join.encode(encoding))
|
||||
|
||||
def environment():
|
||||
currtemp = float(zapi.item.get(filter={'hostid': '10125', 'itemid': '26379'})[0]['lastvalue'])
|
||||
currhumid = calibrate + float(zapi.item.get(filter={'hostid': '10125', 'itemid': '26618'})[0]['lastvalue'])
|
||||
|
||||
t_result = 't=' + str(currtemp) + '°C'
|
||||
if currtemp > 24:
|
||||
t_result += ' !! WARNING !!'
|
||||
t_result += ' h=' + str(currhumid) + '%'
|
||||
t_sock_result = 'PRIVMSG ' + channel + ' :' + t_result + '\n'
|
||||
irc.send(t_sock_result.encode(encoding))
|
||||
|
||||
|
||||
while True:
|
||||
time.sleep(1)
|
||||
counter += 1
|
||||
|
||||
if counter == 60:
|
||||
counter = 0
|
||||
ctime = datetime.datetime.now()
|
||||
if int(ctime.minute) == 20:
|
||||
environment() # print env data every 20 minutes
|
||||
|
||||
triggers = zapi.trigger.get(only_true=1,
|
||||
skipDependent=1,
|
||||
monitored=1,
|
||||
active=1,
|
||||
output='extend',
|
||||
expandDescription=1,
|
||||
expandData='host',
|
||||
sortfield='triggerid'
|
||||
)
|
||||
|
||||
for t in triggers:
|
||||
#print(t)
|
||||
if int(t['value']) == 1:
|
||||
triglist.append("{}".format(t['description'])
|
||||
#triglist.append("{0} - {1}".format(
|
||||
# t['host'],
|
||||
# t['description'],
|
||||
# '')
|
||||
)
|
||||
|
||||
for d in Differ().compare(trigprevlist, triglist):
|
||||
if re.match('^\+', d) is not None:
|
||||
sock_result = 'PRIVMSG ' + nick + " :" + d + '\n'
|
||||
irc.send(sock_result.encode(encoding))
|
||||
if re.match('^\-', d) is not None:
|
||||
sock_result = 'PRIVMSG ' + nick + " :" + d + '\n'
|
||||
irc.send(sock_result.encode(encoding))
|
||||
time.sleep(1)
|
||||
|
||||
trigprevlist = triglist
|
||||
triglist = []
|
||||
|
||||
try:
|
||||
text=irc.recv(2040) #receive the text
|
||||
decoded_text = text.decode(encoding)
|
||||
|
||||
### print (decoded_text) #print text to console DEBUG
|
||||
|
||||
if decoded_text.find('!ping') != -1:
|
||||
triggers = zapi.trigger.get(only_true=1,
|
||||
skipDependent=1,
|
||||
monitored=1,
|
||||
active=1,
|
||||
output='extend',
|
||||
expandDescription=1,
|
||||
expandData='host',
|
||||
sortfield='triggerid'
|
||||
)
|
||||
for t in triggers:
|
||||
if int(t['value']) == 1:
|
||||
result = "{0} - {1} {2}".format(t['host'], t['description'], '')
|
||||
sock_result = 'PRIVMSG ' + channel + " :" + result + '\n'
|
||||
irc.send(sock_result.encode(encoding))
|
||||
time.sleep(1)
|
||||
if decoded_text.find('!env') != -1:
|
||||
environment()
|
||||
|
||||
# Prevent Timeout
|
||||
if decoded_text.find('PING') != -1:
|
||||
pingpong = 'PONG ' + decoded_text.split() [1] + '\r\n'
|
||||
irc.send(pingpong.encode(encoding))
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
exit(0)
|
||||
|
70
tests/getz.py
Executable file
70
tests/getz.py
Executable file
|
@ -0,0 +1,70 @@
|
|||
#!/usr/bin/python3.2
|
||||
#
|
||||
# Zabbix API Python usage example
|
||||
# Christoph Haas <email@christoph-haas.de>
|
||||
#
|
||||
|
||||
username=''
|
||||
password='1'
|
||||
hostgroup=''
|
||||
item_name='system.cpu.load[,avg1]'
|
||||
zabbix_url=''
|
||||
|
||||
import zabbix_api
|
||||
import sys
|
||||
|
||||
# Connect to Zabbix server
|
||||
z=zabbix_api.ZabbixAPI(server=zabbix_url)
|
||||
z.login(user=username, password=password)
|
||||
|
||||
# Get hosts in the hostgroup
|
||||
hostgroup = z.hostgroup.get(
|
||||
{
|
||||
'filter': { 'name':hostgroup },
|
||||
'sortfield': 'name',
|
||||
'sortorder': 'ASC',
|
||||
'limit':2,
|
||||
'select_hosts':'extend'
|
||||
})
|
||||
|
||||
print(hostgroup[0])
|
||||
print("\n")
|
||||
for host in hostgroup[0]['name']:
|
||||
hostname = host['host']
|
||||
print("Host:", hostname)
|
||||
print("Host-ID:", host['hostid'])
|
||||
|
||||
item = z.item.get({
|
||||
'output':'extend',
|
||||
'hostids':host['hostid'],
|
||||
'filter':{'key_':item_name}})
|
||||
if item:
|
||||
print(item[0]['lastvalue'])
|
||||
print("Item-ID:", item[0]['itemid'])
|
||||
|
||||
# Get history
|
||||
lastvalue = z.history.get({
|
||||
'history': item[0]['value_type'],
|
||||
'itemids': item[0]['itemid'],
|
||||
'output': 'extend',
|
||||
# Sort by timestamp from new to old
|
||||
'sortfield':'clock',
|
||||
'sortorder':'DESC',
|
||||
# Get only the first (=newest) entry
|
||||
'limit': 1,
|
||||
})
|
||||
|
||||
# CAVEAT! The history.get function must be told which type the
|
||||
# values are (float, text, etc.). The item.value_type contains
|
||||
# the number that needs to be passed to history.get.
|
||||
if lastvalue:
|
||||
lastvalue = lastvalue[0]['value']
|
||||
|
||||
print("Last value:", lastvalue)
|
||||
|
||||
else:
|
||||
print("No item....")
|
||||
|
||||
print("---------------------------")
|
||||
|
||||
|
2
tests/z-gethostid.sh
Executable file
2
tests/z-gethostid.sh
Executable file
|
@ -0,0 +1,2 @@
|
|||
#!/bin/bash
|
||||
curl -i -X POST -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","method":"host.get","params":{"output":"extend","filter":{"host":"fqdn"}},"auth":"changeme","id":1}' http://fqdn/zabbix/api_jsonrpc.php
|
2
tests/z-getitem.sh
Executable file
2
tests/z-getitem.sh
Executable file
|
@ -0,0 +1,2 @@
|
|||
#!/bin/bash
|
||||
curl -i -X POST -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","method":"item.get","params":{"output":"extend","filter":{"hostid":"10125"}},"auth":"changeme","id":2}' http://fqdn/zabbix/api_jsonrpc.php
|
2
tests/z-getuser.sh
Executable file
2
tests/z-getuser.sh
Executable file
|
@ -0,0 +1,2 @@
|
|||
#!/bin/bash
|
||||
curl -i -X POST -H 'Content-Type:application/json' -d'{"jsonrpc": "2.0","method":"user.login","params":{"user":"USER","password":"PASS"},"auth": null,"id":0}' http://fqdn/zabbix/api_jsonrpc.php
|
Loading…
Reference in a new issue