File: //proc/thread-self/root/etc/zabbix/sower/sower_metrics.py
#!/usr/bin/python3.6
from __future__ import print_function
from __future__ import division
from requests import Session
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
import psutil
import socket
import json
import os
import sys
def GPP(data, url, route, method=None):
endpoint = "http://%s/%s" %(url, route)
header = {}
header["Authorization"] = "Token cb935f472a8af723098d24740d7b3e84213f0f54"
s = Session()
s.trust_env = False
if method == "GET":
return s.get(endpoint, headers=header)
elif method == "POST":
header = {}
header["Content-Type"] = "application/json"
header["Authorization"] = "Token cb935f472a8af723098d24740d7b3e84213f0f54"
return s.post(endpoint, headers=header, data=data)
elif method == "PUT":
header = {}
header["Content-Type"] = "application/json"
header["Authorization"] = "Token cb935f472a8af723098d24740d7b3e84213f0f54"
return s.put(endpoint, headers=header, data=data)
def GET_APACHE(url):
if url is not None:
r = Session()
r.trust_env = False
return r.get(url)
try:
d = {}
server = socket.gethostname()
m = psutil.virtual_memory()
d["name"] = server
d["cpu"] = psutil.cpu_percent(interval=1)
d["memory_used"] = m.percent
d["status"] = "true"
d["elegible"] = "true"
d["idleWorkers"] = [str(values) for values in GET_APACHE("http://localhost/server-status?auto").text.split("\n") if "IdleWorkers" in values][0].split(":")[1].strip()
d["products"] = "new_shared_linux"
d["sites"] = [sites.endswith(".conf") for sites in os.listdir("/etc/locaweb/hospedagem/")].count(True)
if d is not None:
GET = GPP(json.dumps(d),"sower.infralwb.com.br","v2/shared_linux/{0}".format(server),method="GET")
if GET.status_code == 400:
GPP(json.dumps(d),"sower.infralwb.com.br","v2/shared_linux",method="POST")
else:
GPP(json.dumps(d),"sower.infralwb.com.br","v2/shared_linux/{0}".format(server),method="PUT")
print (1)
else:
GET = GPP(json.dumps(d),"sower.infralwb.com.br","v2/shared_linux/{0}".format(server),method="GET")
if GET.status_code == 400:
GPP(json.dumps(d),"sower.infralwb.com.br","v2/shared_linux",method="POST")
else:
GPP(json.dumps(d),"sower.infralwb.com.br","v2/shared_linux/{0}".format(server),method="PUT")
print (1)
except:
print (0)