Skip to content

Instantly share code, notes, and snippets.

@ashishtiwari1993
Last active October 12, 2020 16:36
Show Gist options
  • Save ashishtiwari1993/7fca576c55cce93b8b980cdfcc420744 to your computer and use it in GitHub Desktop.
Save ashishtiwari1993/7fca576c55cce93b8b980cdfcc420744 to your computer and use it in GitHub Desktop.
Prometheus exporter to fetch if any specific service running or stopped.

Service up exporter for prometheus

(by @_ashish_tiwari)


It performs ps -ef | grep service_name | wc -l command and gives response in 1 or 0.

Get Start

  1. Install prometheus client by pip install prometheus_client.
  2. python service_up_exporter.py
  3. Go to localhost:8000/metrics

Sample response

# HELP python_info Python platform information
# TYPE python_info gauge
python_info{implementation="CPython",major="2",minor="7",patchlevel="5",version="2.7.5"} 1.0
# HELP process_virtual_memory_bytes Virtual memory size in bytes.
# TYPE process_virtual_memory_bytes gauge
process_virtual_memory_bytes 2.34754048e+08
# HELP process_resident_memory_bytes Resident memory size in bytes.
# TYPE process_resident_memory_bytes gauge
process_resident_memory_bytes 1.2378112e+07
# HELP process_start_time_seconds Start time of the process since unix epoch in seconds.
# TYPE process_start_time_seconds gauge
process_start_time_seconds 1.56378317373e+09
# HELP process_cpu_seconds_total Total user and system CPU time spent in seconds.
# TYPE process_cpu_seconds_total counter
process_cpu_seconds_total 0.14
# HELP process_open_fds Number of open file descriptors.
# TYPE process_open_fds gauge
process_open_fds 7.0
# HELP process_max_fds Maximum number of open file descriptors.
# TYPE process_max_fds gauge
process_max_fds 1024.0
# HELP service_health_status Check status of service
# TYPE service_health_status gauge
service_health_status{host="ashish",script_name_pattern="myprocess1"} 0.0
service_health_status{host="ashish",script_name_pattern="myprocess2"} 0.0
service_health_status{host="ashish",script_name_pattern="elasticsearch"} 1.0
from prometheus_client import make_wsgi_app, Gauge
from wsgiref.simple_server import make_server
import os, subprocess
# This serivice name will put in grep command
# Example : ps -ef | grep 'myprocess1'
# Kindly Specify process name carefully.
services = ['myprocess1','myprocess2','elasticsearch']
host = os.uname()[1]
g = Gauge('service_health_status', 'Check status of service', ['host','script_name_pattern'])
def generate():
for s in services:
try:
proc1 = subprocess.Popen(['ps', '-ef'], stdout=subprocess.PIPE)
proc2 = subprocess.Popen(['grep', s], stdin=proc1.stdout,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
proc3 = subprocess.Popen(['wc', '-l'], stdin=proc2.stdout,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
proc1.stdout.close() # Allow proc1 to receive a SIGPIPE if proc2 exits.
proc2.stdout.close() # Allow proc2 to receive a SIGPIPE if proc3 exits.
out, err = proc3.communicate()
if err:
g.labels(host,s).set(0)
else:
p=int(out)
if p > 1:
g.labels(host,s).set(1)
else:
g.labels(host,s).set(0)
except:
g.labels(host,s).set(0)
metrics_app = make_wsgi_app()
def my_app(environ, start_fn):
if environ['PATH_INFO'] == '/metrics':
generate()
return metrics_app(environ, start_fn)
httpd = make_server('', 8000, my_app)
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment