summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOleg Pykhalov <go.wigust@gmail.com>2022-02-05 18:51:55 +0300
committerOleg Pykhalov <go.wigust@gmail.com>2022-02-06 03:20:43 +0300
commit6b5eeb6d5ae3866f182b92f92bc8483050ce00c3 (patch)
tree15a67deac324fd3371eb2f5876f646fadf7e1a81
downloadpython-prometheus-ssh-exporter-1.0.0.tar.gz
Initial commit.v1.0.0
-rw-r--r--channels.scm6
-rw-r--r--guix.scm80
-rw-r--r--prometheus_tp_link_exporter/__init__.py0
-rw-r--r--prometheus_tp_link_exporter/__main__.py68
-rw-r--r--setup.py32
5 files changed, 186 insertions, 0 deletions
diff --git a/channels.scm b/channels.scm
new file mode 100644
index 0000000..592fa91
--- /dev/null
+++ b/channels.scm
@@ -0,0 +1,6 @@
+(list (channel
+ (name 'guix)
+ (url "https://git.savannah.gnu.org/git/guix.git")
+ (branch "master")
+ (commit
+ "d96f47f012571cdd6dd67c513e496042db303ca7")))
diff --git a/guix.scm b/guix.scm
new file mode 100644
index 0000000..f1f7b8b
--- /dev/null
+++ b/guix.scm
@@ -0,0 +1,80 @@
+;;; guix.scm --- Guix package for Prometheus TP-Link Exporter
+
+;; Copyright © 2022 Oleg Pykhalov <go.wigust@gmail.com>
+
+;; This file is part of prometheus-tp-link-exporter.
+
+;; prometheus-tp-link-exporter is free software; you can redistribute it
+;; and/or modify it under the terms of the GNU General Public License as
+;; published by the Free Software Foundation, either version 3 of the License,
+;; or (at your option) any later version.
+;;
+;; prometheus-tp-link-exporter is distributed in the hope that it will be
+;; useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
+;; Public License for more details.
+;;
+;; You should have received a copy of the GNU General Public License along
+;; with prometheus-tp-link-exporter. If not, see
+;; <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; This file contains Guix package for development version of
+;; prometheus-tp-link-exporter. To build or install, run:
+;;
+;; guix build --file=guix.scm
+;; guix package --install-from-file=guix.scm
+
+;; The main purpose of this file though is to make a development
+;; environment for building prometheus-tp-link-exporter:
+;;
+;; guix shell --pure
+
+;;; Code:
+
+(use-modules ((guix licenses) #:prefix license:)
+ (gnu packages admin)
+ (gnu packages monitoring)
+ (guix build utils)
+ (guix build-system python)
+ (guix gexp)
+ (guix git-download)
+ (guix packages)
+ (ice-9 popen)
+ (ice-9 rdelim))
+
+(define %source-dir (dirname (current-filename)))
+
+(define (git-output . args)
+ "Execute 'git ARGS ...' command and return its output without trailing
+newspace."
+ (with-directory-excursion %source-dir
+ (let* ((port (apply open-pipe* OPEN_READ "git" args))
+ (output (read-string port)))
+ (close-pipe port)
+ (string-trim-right output #\newline))))
+
+(define (current-commit)
+ (git-output "log" "-n" "1" "--pretty=format:%H"))
+
+(define-public prometheus-tp-link-exporter
+ (let ((commit (current-commit)))
+ (package
+ (name "prometheus-tp-link-exporter")
+ (version (string-append "1.0.0" "-" (string-take commit 7)))
+ (source (local-file %source-dir
+ #:recursive? #t
+ #:select? (git-predicate %source-dir)))
+ (build-system python-build-system)
+ (arguments
+ '(#:tests? #f)) ; no tests
+ (propagated-inputs
+ (list jc python-prometheus-client))
+ (home-page "https://gitlab.com/wigust/prometheus-tp-link-exporter")
+ (synopsis "Prometheus TP-Link Exporter")
+ (description
+ "This package provides Prometheus TP-Link Exporter.")
+ (license license:gpl3+))))
+
+prometheus-tp-link-exporter
diff --git a/prometheus_tp_link_exporter/__init__.py b/prometheus_tp_link_exporter/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/prometheus_tp_link_exporter/__init__.py
diff --git a/prometheus_tp_link_exporter/__main__.py b/prometheus_tp_link_exporter/__main__.py
new file mode 100644
index 0000000..d8a0d8a
--- /dev/null
+++ b/prometheus_tp_link_exporter/__main__.py
@@ -0,0 +1,68 @@
+"""Connect to TP-Link router over SSH and collect metrics."""
+
+from prometheus_client import Counter, start_http_server
+import jc.parsers.ifconfig
+import os
+import subprocess
+import time
+
+host = os.getenv("PROMETHEUS_TP_LINK_EXPORTER_HOST", "")
+password = os.getenv("PROMETHEUS_TP_LINK_EXPORTER_PASSWORD", "")
+interval = int(os.getenv("PROMETHEUS_TP_LINK_EXPORTER_INTERVAL", "10"))
+listen_port = int(os.getenv("PROMETHEUS_TP_LINK_EXPORTER_LISTEN_PORT", "9101"))
+
+
+def ifconfig_devices():
+ """Connect to TP-Link device over SSH and return ifconfig output."""
+ return jc.parsers.ifconfig.parse(
+ subprocess.getoutput(
+ f"sshpass -p{password} ssh -o KexAlgorithms=+diffie-hellman-group1-sha1 -F /dev/null admin@{host} /sbin/ifconfig"
+ )
+ )
+
+
+node_network_receive_bytes_total = Counter(
+ "node_network_receive_bytes_total",
+ "Network device statistic receive_bytes.",
+ ["device"],
+)
+
+
+def process_node_network_receive_bytes_total():
+ """Process request."""
+ for device in ifconfig_devices():
+ node_network_receive_bytes_total.labels(device=device["name"]).inc(
+ device["rx_bytes"]
+ - node_network_receive_bytes_total._metrics[(device["name"],)]._value._value
+ )
+
+
+node_network_transmit_bytes_total = Counter(
+ "node_network_transmit_bytes_total",
+ "Network device statistic transmit_bytes.",
+ ["device"],
+)
+
+
+def process_node_network_transmit_bytes_total():
+ """Process request."""
+ for device in ifconfig_devices():
+ node_network_transmit_bytes_total.labels(device=device["name"]).inc(
+ device["tx_bytes"]
+ - node_network_transmit_bytes_total._metrics[
+ (device["name"],)
+ ]._value._value
+ )
+
+
+def main():
+ """Entry point."""
+ start_http_server(9101)
+ while True:
+ process_node_network_receive_bytes_total()
+ process_node_network_transmit_bytes_total()
+ time.sleep(interval)
+
+
+if __name__ == "__main__":
+ main()
diff --git a/setup.py b/setup.py
new file mode 100644
index 0000000..ba0fbd8
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,32 @@
+import sys
+from setuptools import setup
+
+MAJOR_VERSION = '1'
+MINOR_VERSION = '0'
+MICRO_VERSION = '0'
+VERSION = "{}.{}.{}".format(MAJOR_VERSION, MINOR_VERSION, MICRO_VERSION)
+
+setup(name='prometheus_tp_link_exporter',
+ version=VERSION,
+ description="Prometheus TP-Link exporter.",
+ url='https://gitlab.com/wigust/prometheus-tp-link-exporter',
+ author='Oleg Pykhalov',
+ author_email='go.wigust@gmail.com',
+ license='GPLv3',
+ packages=['prometheus_tp_link_exporter'],
+ classifiers=[
+ 'Environment :: Console',
+ 'Intended Audience :: End Users/Desktop',
+ 'Intended Audience :: Developers',
+ 'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
+ 'Operating System :: MacOS :: MacOS X',
+ 'Operating System :: Unix',
+ 'Operating System :: POSIX',
+ 'Programming Language :: Python',
+ 'Programming Language :: Python :: 3.6',
+ 'Development Status :: 5 - Production/Stable',
+ 'Topic :: Office/Business'
+ ],
+ zip_safe=False,
+ entry_points={'console_scripts': ['prometheus-tp-link-exporter = prometheus_tp_link_exporter.__main__:main']},
+ platforms='any')