Code Monkey home page Code Monkey logo

ping-exporter's Introduction

ping-exporter

Introduction

Prometheus Ping Exporter is a simple python script which utilize fping to probe endpoint through ICMP and parsing the output to Prometheus. The result can then be visualize through Grafana with ease.

Requirements

  • Python 2.x
  • fping 4.x

Screenshots alt text

For Debian user, you can get the fping deb file from http://ftp.debian.org/debian/pool/main/f/fping/fping_4.0-1_armhf.deb

For docker user, you can build the container with the Docker file (based on alpine around 54M). For arm user, you can change FROM to "armhf/alpine:edge"

PS: The script is working fine with > 40 ping target in a PI 3B.

Getting Started

  1. Download ping-exporter.py and place it inside /opt/
# cd /opt/
# curl  -O https://raw.githubusercontent.com/frankiexyz/ping-exporter/master/ping-exporter.py
  1. Ensure Correct Permission on ping-exporter.py
# chmod 755 /opt/ping-exporter.py
  1. Running The Ping Exporter
# /usr/bin/python /opt/ping-exporter.py
  1. Testing The Script
# curl "127.0.0.1:8085/?target=8.8.8.8"

Running Script On System Startup

CentOS 7 (Using Systemd)

  1. Create a new ping_exporter.service file at /lib/systemd/system/
vi /lib/systemd/system/ping_exporter.service
  1. Paste The Following into ping_exporter.service
[Unit]
Description=Ping Exporter for Prometheus (Created By Frankie)
After=multi-user.target

[Service]
Type=idle
ExecStart=/usr/bin/python /opt/ping-exporter.py

[Install]
WantedBy=multi-user.target
  1. Save The File and Execute systemctl daemon-reload
# systemctl daemon-reload
  1. Start and Enable Ping Exporter Service
# systemctl start ping_exporter.service
# systemctl enable ping_exporter.service

Configuration

Prometheus Configuration

Append the following in prometheus's config (Default is prometheus.yml)

  - job_name: 'ping-exporter'
    scrape_interval: 60s
    metrics_path: /probe
    params:
         prot: ['4']
    static_configs:
      - targets:
          - www.ifconfig.xyz
          - www.google.com
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
        replacement: ${1}
      - source_labels: [__param_target]
        regex: (.*)
        target_label: instance
        replacement: ${1}
      - source_labels: []
        regex: .*
        target_label: __address__
        replacement: <Your exporter IP>:8085

You might want to add or change the following parameters in params's section to match your requirements

    params:
         # Default Is IPv4, Can Be Changed To IPv6
         prot: ['4']

         # Ping Packet Size (Default value is 56)
         size: ['56']

         # Ping Count (Default value is 10 times)
         count: ['10']

         # Ping Interval (Default value is 500ms)
         interval: ['500']

         # Source address for ping (System default used if not specified)
         source: ['10.10.10.10']

Prometheus configuration example where the job pings a single destination (params:target) but from multiple source addresses (static_config:targets) to determine the quality of each route / path:

- job_name: 'ping-exporter'
  scrape_interval: 60s
  metrics_path: /probe
  params:
       prot: ['4']
       count: ['3']
       target: ['207.225.112.9']
  static_configs:
    - targets:
        - 192.168.101.2
        - 192.168.102.2
        - 192.168.103.2
        - 192.168.104.2
  relabel_configs:
    - source_labels: [__address__]
      target_label: __param_source
      replacement: ${1}
    - source_labels: [__param_target]
      regex: (.*)
      target_label: instance
      replacement: ${1}
    - source_labels: [__param_source]
      regex: (.*)
      target_label: source
      replacement: ${1}
    - source_labels: []
      regex: .*
      target_label: __address__
      replacement: localhost:8085

ping-exporter's People

Contributors

caimaoy avatar chriscook8 avatar frankiexyz avatar hmmxp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

ping-exporter's Issues

Error

Hi,

Running ping-exporter.py on Centos 7.4 on 1.1.1.1 and Prometheus 1.7.2 on 1.1.1.2

On 1.1.1.1 Running Ping Exporter Receiving The Following Error

Exception happened during processing of request from ('1.1.1.2', 44436)
Traceback (most recent call last):
File "/usr/lib64/python2.7/SocketServer.py", line 593, in process_request_thread
self.finish_request(request, client_address)
File "/usr/lib64/python2.7/SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib64/python2.7/SocketServer.py", line 649, in init
self.handle()
File "/usr/lib64/python2.7/BaseHTTPServer.py", line 340, in handle
self.handle_one_request()
File "/usr/lib64/python2.7/BaseHTTPServer.py", line 328, in handle_one_request
method()
File "/opt/lis/ping-exporter.py", line 48, in do_GET
message = '\n'.join(ping(address, False, count, size))
File "/opt/lis/ping-exporter.py", line 18, in ping
loss = cmd_output[1].split("%")[1].split("/")[2]
IndexError: list index out of range

I Am Running Ping Exporter via
/usr/bin/python /opt/lis/ping-exporter.py

My Configuration On Prometheus

  • job_name: 'ping_exporter'
    scrape_interval: 60s
    metrics_path: /probe
    params:
    prot: ['4']
    static_configs:
    • targets:
    • source_labels: [address]
      target_label: __param_target
      replacement: ${1}
    • source_labels: [__param_target]
      regex: (.*)
      target_label: instance
      replacement: ${1}
    • source_labels: []
      regex: .*
      target_label: address
      replacement: 1.1.1.1:8085

How Can I Resolve or Run This Script Properly

Thank You

Exporter hung but didn't exit

I'm running the ping exporter as a systemd service as suggested in the readme.

I noticed in my grafana dashboard that the ping stats had stopped.

systemd status show the service is still running, but the last logs show an exception:

sudo systemctl status ping_exporter.service
● ping_exporter.service - Ping Exporter for Prometheus (Created By Frankie)
Loaded: loaded (/etc/systemd/system/ping_exporter.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2021-06-06 22:33:36 BST; 1 weeks 6 days ago
Main PID: 1112248 (python)
Tasks: 1 (limit: 9239)
Memory: 8.9M
CGroup: /system.slice/ping_exporter.service
└─1112248 /usr/bin/python /opt/ping-exporter.py

Jun 16 23:14:39 micro python[1112248]: self.RequestHandlerClass(request, client_address, self)
Jun 16 23:14:39 micro python[1112248]: File "/usr/lib/python2.7/SocketServer.py", line 657, in init
Jun 16 23:14:39 micro python[1112248]: self.finish()
Jun 16 23:14:39 micro python[1112248]: File "/usr/lib/python2.7/SocketServer.py", line 716, in finish
Jun 16 23:14:39 micro python[1112248]: self.wfile.close()
Jun 16 23:14:39 micro python[1112248]: File "/usr/lib/python2.7/socket.py", line 283, in close
Jun 16 23:14:39 micro python[1112248]: self.flush()
Jun 16 23:14:39 micro python[1112248]: File "/usr/lib/python2.7/socket.py", line 307, in flush
Jun 16 23:14:39 micro python[1112248]: self._sock.sendall(view[write_offset:write_offset+buffer_size])
Jun 16 23:14:39 micro python[1112248]: error: [Errno 32] Broken pipe
chibbert@micro:~$ sudo journalctl ping_exporter.service

journalctl logs suggest something went bad and never recovered:
Jun 16 23:14:34 micro python[1112248]: 127.0.0.1 - - [16/Jun/2021 23:14:34] "GET /probe?prot=4&target=www.google.co.uk HTTP/1.1" 200 -
Jun 16 23:14:34 micro python[1112248]: 2021-06-16 23:14:34,994 root INFO /usr/bin/fping -4 -b 56 -i 1 -p 500 -q -c 10 www.google.co.uk
Jun 16 23:14:39 micro python[1112248]: 127.0.0.1 - - [16/Jun/2021 23:14:39] "GET /probe?prot=4&target=www.google.co.uk HTTP/1.1" 200 -
Jun 16 23:14:39 micro python[1112248]: Traceback (most recent call last):
Jun 16 23:14:39 micro python[1112248]: File "/usr/lib/python2.7/SocketServer.py", line 599, in process_request_thread
Jun 16 23:14:39 micro python[1112248]: self.finish_request(request, client_address)
Jun 16 23:14:39 micro python[1112248]: File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
Jun 16 23:14:39 micro python[1112248]: self.RequestHandlerClass(request, client_address, self)
Jun 16 23:14:39 micro python[1112248]: File "/usr/lib/python2.7/SocketServer.py", line 657, in init
Jun 16 23:14:39 micro python[1112248]: self.finish()
Jun 16 23:14:39 micro python[1112248]: File "/usr/lib/python2.7/SocketServer.py", line 716, in finish
Jun 16 23:14:39 micro python[1112248]: self.wfile.close()
Jun 16 23:14:39 micro python[1112248]: File "/usr/lib/python2.7/socket.py", line 283, in close
Jun 16 23:14:39 micro python[1112248]: self.flush()
Jun 16 23:14:39 micro python[1112248]: File "/usr/lib/python2.7/socket.py", line 307, in flush
Jun 16 23:14:39 micro python[1112248]: self._sock.sendall(view[write_offset:write_offset+buffer_size])
Jun 16 23:14:39 micro python[1112248]: error: [Errno 32] Broken pipe

Python exception error

Hello
I'm in the process of configuring ping exporter and the issue i'm facing is the metric values are not populated on Prometheus (metrics are available). If I access http://IP:8085/probe?prot=4&target=target_ip in the browser i see the values as below:

ping_avg 21.0
ping_max 21.9
ping_min 20.6
ping_loss 0

Prometheus config looks like this
'''

  • job_name: 'ping-exporter'
    scrape_interval: 60s
    metrics_path: /probe
    params:
    prot: ['4']
    static_configs:
    - targets:
    - target_ip
    relabel_configs:
    - source_labels: [address]
    target_label: __param_target
    - source_labels: [__param_target]
    target_label: instance
    - target_label: address
    replacement: Prom_IP:8085

'''
As i run the exporter I see below exception :

2019-09-21 09:44:29,097 root INFO /usr/bin/fping -4 -b 56 -i 1 -p 500 -q -c 10 Target_ip
client_ip - - [21/Sep/2019 09:44:33] "GET /probe?prot=4&target=target_ip HTTP/1.1" 200 -

Exception happened during processing of request from ('Prom_IP', 64464)
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 596, in process_request_thread
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 331, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python2.7/SocketServer.py", line 652, in init
self.handle()
File "/usr/lib/python2.7/BaseHTTPServer.py", line 340, in handle
self.handle_one_request()
File "/usr/lib/python2.7/BaseHTTPServer.py", line 328, in handle_one_request
method()
File "/opt/ping-exporter.py", line 58, in do_GET
address = value['target'][0]
KeyError: 'target'

Would like to know why are the values not populating on Prometheus metrics. Kindly help!

Thanks

Command injection vulnerability

This exporter, while nice to have, opens the server it is running on to code injection vulnerabilities as the command line parameters are not escaped and executed as-is.

Try it yourself:

curl "localhost:8085?target=\`touch%20iamnotsupposedtobehere\`"

You will then find a file where the .py script is located: iamnotsupposedtobehere

Name of metric

I added this python code onto my server and configured Prometheus to connect to it but now I don't know what is the name of metric to add to be shown on my Grafana console.

optimizing multiple targets performance

Hello,

Is't possible to optimize multiple targets performance ?

What I've in mind is when you specify multiple hosts in Prometheus configuration.

It'd be nice to have an exporter modified to put all targets into one fping line for example:

/usr/local/sbin/fping -4 -b 1200 -i 1 -p 100 -q -c 100 10.1.14.1 10.1.14.2 10.1.14.3 10.1.14.4 10.1.14.5 10.1.14.6

instead of this:

/usr/local/sbin/fping -4 -b 1200 -i 1 -p 100 -q -c 100 10.1.14.1
/usr/local/sbin/fping -4 -b 1200 -i 1 -p 100 -q -c 100 10.1.14.2
/usr/local/sbin/fping -4 -b 1200 -i 1 -p 100 -q -c 100 10.1.14.3
/usr/local/sbin/fping -4 -b 1200 -i 1 -p 100 -q -c 100 10.1.14.4
/usr/local/sbin/fping -4 -b 1200 -i 1 -p 100 -q -c 100 10.1.14.5
/usr/local/sbin/fping -4 -b 1200 -i 1 -p 100 -q -c 100 10.1.14.6

is't possible ? Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.