Skip to content

Instantly share code, notes, and snippets.

@RPDiep
Created July 21, 2014 12:05
Show Gist options
  • Save RPDiep/388be0184c7c8caefcd3 to your computer and use it in GitHub Desktop.
Save RPDiep/388be0184c7c8caefcd3 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import socket
import subprocess
import sys
import tempfile
import urllib2
url='http://127.0.0.1/server-status?auto'
zabbixhost='zabbix'
zabbixsender='/usr/bin/zabbix_sender'
### http://stackoverflow.com/questions/788411/check-to-see-if-python-script-is-running ###
def get_lock(process_name):
global lock_socket
lock_socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
try:
lock_socket.bind('\0' + process_name)
except socket.error:
print 'Apachestats already running, exiting'
sys.exit(1)
get_lock('apachestats')
##########################################################################################
def spawn( command ):
p = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
(result, err) = p.communicate()
result = result.strip()
return result
socket.setdefaulttimeout(5)
hostname=socket.getfqdn()
tmpfile=tempfile.NamedTemporaryFile(dir='/tmp', delete=True)
result=urllib2.urlopen(url).read()
### http://stackoverflow.com/questions/5783417/using-python-to-parse-colon-delimited-string-to-an-object ###
serverstatus=dict(map(str.strip, line.split(':', 1)) for line in result.splitlines())
try:
tmpfile.write("%s apache-extended.accesses %s\n" % ( hostname,serverstatus['Total Accesses'] ))
except KeyError:
pass
try:
tmpfile.write("%s apache-extended.totalkb %s\n" % ( hostname,serverstatus['Total kBytes'] ))
except KeyError:
pass
try:
tmpfile.write("%s apache-extended.cpuload %s\n" % ( hostname,serverstatus['CPULoad'] ))
except KeyError:
pass
try:
tmpfile.write("%s apache-extended.uptime %s\n" % ( hostname,serverstatus['Uptime'] ))
except KeyError:
pass
try:
tmpfile.write("%s apache-extended.reqpersec %s\n" % ( hostname,serverstatus['ReqPerSec'] ))
except KeyError:
pass
try:
tmpfile.write("%s apache-extended.bytespersec %s\n" % ( hostname,serverstatus['BytesPerSec'] ))
except KeyError:
pass
try:
tmpfile.write("%s apache-extended.bytesperrq %s\n" % ( hostname,serverstatus['BytesPerReq'] ))
except KeyError:
pass
try:
tmpfile.write("%s apache-extended.busyworkers %s\n" % ( hostname,serverstatus['BusyWorkers'] ))
except KeyError:
pass
try:
tmpfile.write("%s apache-extended.idleworkers %s\n" % ( hostname,serverstatus['IdleWorkers'] ))
except KeyError:
pass
tmpfile.flush()
command="%s -z %s -s %s -i %s" % ( zabbixsender, zabbixhost, hostname, tmpfile.name )
spawn(command)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment