Skip to content

Instantly share code, notes, and snippets.

@rsff
Last active March 28, 2021 11:53
Show Gist options
  • Save rsff/11f91e335013ba29726a to your computer and use it in GitHub Desktop.
Save rsff/11f91e335013ba29726a to your computer and use it in GitHub Desktop.
stash pooler
#this script can NEVER FAIL!
'''
This escripte will query zabbix for webscenarios, find out their ID and get the
status of itafterward will update the stashboard
KKTHXBYE
'''
import urllib
import urllib2
import socket
import itertools
import urlparse
import oauth2 as oauth
from pyzabbix import ZabbixAPI
from urlparse import urlparse
from google.appengine.api import urlfetch
from google.appengine.api.urlfetch import DownloadError
from google.appengine.ext import db
from models import Status, Service, Event
from datetime import datetime, timedelta, date
servu = ''
username = ''
password = ''
def massage(data):
data = data[:-1].split(',')[-1]
return data
def get_itemsid():
lista = []
dik = {}
zapi = ZabbixAPI(server=servu)
zapi.session.verify = False
zapi.login(username, password)
stats = zapi.item.get(output='extend', hostids='10172',
webitems='true', search={'key_':"web.test.rspcode"})
for i in stats:
lista.extend((massage(i.get('key_')), i.get('itemid')))
dik = dict(itertools.izip_longest(*[iter(lista)] * 2, fillvalue=""))
return dik
#propably giving this function more love so it can handle subdomains more strongly
def populate_servers(dicto):
serv = []
for key, value in dicto.items():
tmpurl = urlparse(key)
if tmpurl.hostname.split('.')[0] == 'www':
tempurl=tmpurl.hostname.split('.')[1]
else:
tempurl=tmpurl.hostname.split('.')[0]
b = {'service': tempurl, 'url': tmpurl.geturl()}
serv.append(b)
return serv
def get_status(item):
zapi = ZabbixAPI(server=servu)
zapi.session.verify = False
zapi.login(username, password)
stat = zapi.history.get(itemids=item, sortorder='DESC',
sortfield='clock', limit='1')
return stat[0]['value']
def serverisup(service):
service = Service.get_by_slug(service)
status = Status.get_by_slug("up")
e = Event(service=service, status=status, message="The server is responding.")
e.put()
def serverisdown(service):
service = Service.get_by_slug(service)
status = Status.get_by_slug("down")
e = Event(service=service, status=status, message="The server could not be reached")
e.put()
def serveriswarn(service):
service = Service.get_by_slug(service)
status = Status.get_by_slug("warning")
e = Event(service=service, status=status, message="The server is having problems")
e.put()
def populate_server(servers):
consumer_key='anonymous'
consumer_secret='anonymous'
oauth_key='ACCESS_TOKEN'
oauth_secret='ACCESS_SECRET'
base_url="http://:8080/admin/api/v1"
consumer = oauth.Consumer(key=consumer_key,secret=consumer_secret)
token = oauth.Token(oauth_key,oauth_secret)
client = oauth.Client(consumer,token=token)
for val in servers:
data= urllib.urlencode({'name':val['service'],'description':'This is service'+val['service'],})
client.request(base_url+'/services','POST',body=data)
def check(iten, servers):
results = {}
try:
for key, value in iten.items():
for val in servers:
if key == val['url']:
results[val['service']] = get_status(value)
except Exception as e:
print e
for k, v in results.items():
if v == '200':
serverisup(k)
elif v == '500':
serverisdown(k)
else:
serverisdown(k)
gotitems = get_itemsid()
servers = populate_servers(gotitems)
check(gotitems, servers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment