changeset 253:67fb7b27bfea

maildir file count service
author drewp@bigasterisk.com
date Tue, 09 Jan 2024 19:56:01 -0800
parents b97de8433fe1
children 11b738d4c4ae
files mail.py templates/file-count/file-count.service.j2 templates/file-count/file_count.py
diffstat 3 files changed, 40 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mail.py	Mon Jan 08 18:41:08 2024 -0800
+++ b/mail.py	Tue Jan 09 19:56:01 2024 -0800
@@ -35,6 +35,11 @@
         "cd /home/drewp/mbsync; /usr/bin/mbsync-get-cert 10.5.0.1 > servercert",
     ])
 
+    files.put(src='templates/file-count/file_count.py', dest='/opt/file_count.py')
+    files.template(src='templates/file-count/file-count.service.j2', dest='/etc/systemd/system/maildir-count.service')
+    systemd.service(service='maildir-count.service', enabled=True, running=True, daemon_reload=True)
+
+
 # other machines, route mail to bang or prime for delivery
 
 if host.name == 'bang':
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/templates/file-count/file-count.service.j2	Tue Jan 09 19:56:01 2024 -0800
@@ -0,0 +1,15 @@
+# written by pyinfra
+
+[Unit]
+After=wg-quick@wg0.service
+
+[Service]
+Type=exec
+KillMode=process
+# port 2500 is used in victoriametrics/config/scrape_main.yaml
+ExecStart=runuser -u drewp /usr/bin/python3 /opt/file_count.py 10.5.0.2 2500 /home/drewp/Maildir/new maildir_count
+Restart=always
+RestartSec=5s
+
+[Install]
+WantedBy=multi-user.target
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/templates/file-count/file_count.py	Tue Jan 09 19:56:01 2024 -0800
@@ -0,0 +1,20 @@
+import http.server
+import socketserver
+import os
+import sys
+
+interface, port, dir, metric_name = sys.argv[1:]
+
+
+class Web(http.server.SimpleHTTPRequestHandler):
+
+    def do_GET(self):
+        files_count = len(os.listdir(dir))
+        self.send_response(200)
+        self.send_header('Content-type', 'text/plain')
+        self.end_headers()
+        self.wfile.write(f'{metric_name} {files_count}'.encode())
+
+
+with socketserver.TCPServer((interface, int(port)), Web) as httpd:
+    httpd.serve_forever()