David 9 éve
commit
2de74febdd
3 módosított fájl, 202 hozzáadás és 0 törlés
  1. 48 0
      analytics.sh
  2. 58 0
      cert-checker.py
  3. 96 0
      parse_iftop.py

+ 48 - 0
analytics.sh

@@ -0,0 +1,48 @@
+#!/bin/bash
+deh() {
+	echo $1 | awk '/[0-9]$/{print $1;next} /MB$/{printf "%u\n", $1*(1024*1024);next} /KB$/{printf "%u\n", $1*1024}' 
+}
+
+function log {
+	curl -s -i -XPOST 'http://graphs.eba:8086/write?db=curso' --data-binary "$1" >/dev/null
+}
+
+function online {
+	cuantos=$(ssh especificosba.com.ar './cuantos_online.sh')
+	log "curso,type=online value=$cuantos"
+}
+
+function drops {
+	drops=$(ssh especificosba.com.ar "find /var/videos -type f -mmin -10 | wc -l")
+	drops=$(echo $drops-1 | bc)
+	if [ $drops -lt 0 ]; then
+		drops=0
+	fi
+	log "curso,type=drops value=$drops"
+}
+
+function bw {
+	running=$(ps aux | grep voctocore | grep -vc grep)
+	if [ $running -ne 1 ]; then
+		sleep 60
+		return
+	fi
+	TIME=10
+
+	tmp=$(mktemp)
+	#$6 is accumulated, $5 is avg over the last (up to) 40s
+	iftop -P -b -B -n -i vmbr0 -N -t -s $TIME 2>/dev/null >$tmp
+	python3 /root/parse_iftop.py $tmp |\
+	while read -r line; do
+		ip=$(echo $line | cut -d' ' -f 1)
+		data=$(echo $line | cut -d' ' -f 2)
+		log "curso,type=bw,host=${ip} value=${data}"
+	done
+
+	rm $tmp
+}
+
+online &
+bw &
+drops &
+wait

+ 58 - 0
cert-checker.py

@@ -0,0 +1,58 @@
+#!/usr/bin/env python3
+import datetime
+import socket
+import ssl
+import requests
+
+
+def get_cert(domain):
+    context = ssl.create_default_context()
+    conn = context.wrap_socket(
+        socket.socket(socket.AF_INET),
+        server_hostname=domain,
+    )
+    # 3 second timeout because Lambda has runtime limitations
+    conn.settimeout(3.0)
+
+    conn.connect((domain, 443))
+    return conn.getpeercert()
+
+
+def ssl_alt_names(ssl_info):
+    return [item[1] for item in ssl_info['subjectAltName']]
+
+
+def ssl_valid_time_remaining(ssl_info):
+    ssl_date_fmt = r'%b %d %H:%M:%S %Y %Z'
+    expires = datetime.datetime.strptime(ssl_info['notAfter'], ssl_date_fmt)
+    return expires - datetime.datetime.utcnow()
+
+
+def check_domain(domain):
+    cert = get_cert(domain)
+    t_remaining = ssl_valid_time_remaining(cert)
+    return t_remaining.days + t_remaining.seconds / 86400
+
+
+def post_data(domain, time):
+    db = 'scripts_data'
+    INFLUXDB_HOST = "http://grafana.labs:8086/write?db=%s" % db
+    data = 'days_remaining,hostname=%s value=%.2f' % (domain, time)
+    headers = {'Content-Type': 'application/octet-stream'}
+    r = requests.post(INFLUXDB_HOST, data=data, headers=headers)
+    if r.status_code < 200 or r.status_code > 300:
+        print("When posting %s I got status_code %d" % (domain, r.status_code))
+
+
+hosts = [ 'labs.davidventura.com.ar', 'especificosba.com.ar', 'casa.especificosba.com.ar' ]
+data = []
+for h in hosts:
+	try:
+		t = check_domain(h)
+		data.append((h,t))
+	except Exception as e:
+		print("Exception while contacting %s" % h)
+		print(e)
+
+for d in data:
+	post_data(*d)

+ 96 - 0
parse_iftop.py

@@ -0,0 +1,96 @@
+#!/usr/bin/env python3
+import re
+import sys
+import os
+
+matching_ips = re.compile(r"(?P<ip>\d+\.\d+\.\d+\.\d+:\d+)")
+
+data_in = [
+	"192.168.2.208:5000",
+	"192.168.2.208:5002",
+	"192.168.2.209:5000",
+	"192.168.2.79"
+	]
+data_out = [
+	"192.168.2.120:1935"
+]
+
+prev = ''
+
+def dehumanize(value):
+	value = value.strip()
+	if value.endswith("KB"):
+		value = float(value[:-2])*1024
+	elif value.endswith("MB"):
+		value = float(value[:-2])*1024*1024
+	elif value.endswith("B"):
+		value = float(value[:-1])
+		
+	return int(value)
+
+def parse_res(res):
+	if res is None:
+		return
+
+	out = [ o for o in res.split(" ") if len(o) > 1 ]
+	for d in data_in:
+		if d in out[0]:
+			out[0] = d
+			break
+
+	return("%s %s" % (out[0], dehumanize(out[4])))
+
+def parse_file(fname):
+	data=open(fname,"r").readlines()
+	out = []
+	for line in data:
+		match = matching_ips.search(line)
+		if match is None:
+			continue
+	
+		res = None
+		line = line.rstrip()
+		for d in data_in:
+			if d in line:
+				res = line
+		
+		for d in data_out:
+			if d in line:
+				res = matching_ips.sub(d, prev)
+		
+		prev = line
+		ret = parse_res(res)
+		if ret is not None:
+			out.append(ret)
+	
+	for i in data_in:
+		valid = False
+		for o in out:
+			if i in o:
+				valid = True
+		if not valid:
+			out.append("%s 0" % i)
+
+	for i in data_out:
+		valid = False
+		for o in out:
+			if i in o:
+				valid = True
+		if not valid:
+			out.append("%s 0" % i)
+
+	return out
+
+if __name__ == '__main__':
+	if len(sys.argv) != 2:
+		print("Usage: %s file" % sys.argv[0])
+		sys.exit(1)
+
+	f = sys.argv[1]
+	if not os.path.isfile(f):
+		print("%s is not a valid file" % f)
+		sys.exit(2)
+
+	for o in parse_file(f):
+		print(o)
+