Created
August 7, 2016 05:03
-
-
Save bwann/eff6392d115e4beb03df8ffc8f26d8c0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
securityspy.earliest_video 1470546202 458.820833333 camera=kitchen | |
securityspy.camera_bytes 1470546202 7932361974 camera=kitchen type=motion | |
securityspy.camera_bytes 1470546202 6560247008 camera=kitchen type=capture | |
securityspy.camera_mbytes 1470546202 7564 camera=kitchen type=motion | |
securityspy.camera_mbytes 1470546202 6256 camera=kitchen type=capture | |
securityspy.earliest_video 1470546202 309.056111111 camera=catcam | |
securityspy.camera_bytes 1470546202 3524686937 camera=catcam type=motion | |
securityspy.camera_bytes 1470546202 55904854616 camera=catcam type=capture | |
securityspy.camera_mbytes 1470546202 3361 camera=catcam type=motion | |
securityspy.camera_mbytes 1470546202 53315 camera=catcam type=capture | |
securityspy.earliest_video 1470546202 458.056111111 camera=dome | |
securityspy.camera_bytes 1470546202 52751892196 camera=dome type=motion | |
securityspy.camera_bytes 1470546202 796195719712 camera=dome type=capture | |
securityspy.camera_mbytes 1470546202 50308 camera=dome type=motion | |
securityspy.camera_mbytes 1470546202 759311 camera=dome type=capture | |
securityspy.earliest_video 1470546202 458.056111111 camera=balcony | |
securityspy.camera_bytes 1470546202 4403203086 camera=balcony type=motion | |
securityspy.camera_bytes 1470546202 123395003128 camera=balcony type=capture | |
securityspy.camera_mbytes 1470546202 4199 camera=balcony type=motion | |
securityspy.camera_mbytes 1470546202 117678 camera=balcony type=capture | |
securityspy.earliest_video 1470546202 456.056111111 camera=desk_camera | |
securityspy.camera_bytes 1470546202 10410984286 camera=desk_camera type=motion | |
securityspy.camera_bytes 1470546202 6997222938 camera=desk_camera type=capture | |
securityspy.camera_mbytes 1470546202 9928 camera=desk_camera type=motion | |
securityspy.camera_mbytes 1470546202 6673 camera=desk_camera type=capture |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"capture_dirs": [ | |
"/mnt/cameras/SecuritySpy Captured Files" | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
# Collector for SecuritySpy | |
# | |
# vim: syntax=python:expandtab:shiftwidth=4:softtabstop=4:tabstop=4 | |
import glob | |
import json | |
import os | |
import pwd | |
import re | |
import sys | |
import time | |
USER = "nobody" | |
def drop_privileges(): | |
"""Drops privileges if running as root.""" | |
if USER == 'root': | |
return | |
try: | |
ent = pwd.getpwnam(USER) | |
except KeyError: | |
return | |
if os.getuid() != 0: | |
return | |
os.setgid(ent.pw_gid) | |
os.setuid(ent.pw_uid) | |
def print_stat(metric, ts, value, tags=""): | |
if value is not None: | |
print "securityspy.%s %d %s %s" % (metric, ts, value, tags) | |
def do_dir(dir, ts): | |
camera_dirs = glob.glob(os.path.join(dir, '*')) | |
for camera_dir in camera_dirs: | |
camera_name = os.path.basename(camera_dir) | |
# camera_label = str(camera_name).sub('\s', '') | |
camera_label = re.sub('\s', '_', str(camera_name).lower()) | |
by_day = glob.glob(os.path.join(camera_dir, '*-*-*')) | |
camera_earliest = None | |
camera_ago = None | |
camera_total_m_bytes = 0 | |
camera_total_c_bytes = 0 | |
camera_total_bytes = 0 | |
for day in by_day: | |
by_file = glob.glob(os.path.join(day, '*.m?v')) | |
for filename in by_file: | |
name = os.path.basename(filename) | |
# date, time/hour, type, camera.ext | |
m = re.search("([0-9-]+)\s(.*)\s(M|MD|C|TL)\s", name) | |
if m: | |
# print "fn: '%s' '%s' '%s' '%s'" % (name, m.group(1), m.group(2), m.group(3)) | |
f_date = m.group(1) | |
f_timehour = m.group(2) | |
f_type = m.group(3) | |
file_epoch = parse_timestamp(f_date, f_timehour) | |
file_size = os.path.getsize(filename) | |
if camera_earliest: | |
if file_epoch < camera_earliest: | |
camera_earliest = file_epoch | |
else: | |
camera_earliest = file_epoch | |
camera_total_bytes = camera_total_bytes + file_size | |
if f_type == 'M' or f_type == 'MD': | |
camera_total_m_bytes = camera_total_m_bytes + file_size | |
if f_type == 'C' or f_type == 'TL': | |
camera_total_c_bytes = camera_total_c_bytes + file_size | |
if camera_earliest: | |
camera_ago = (ts - camera_earliest) / 3600 | |
if camera_earliest: | |
print_stat('earliest_video', ts, camera_ago, 'camera=' + camera_label) | |
print_stat('camera_bytes', ts, camera_total_m_bytes, 'camera=' + camera_label + ' type=motion') | |
print_stat('camera_bytes', ts, camera_total_c_bytes, 'camera=' + camera_label + ' type=capture') | |
print_stat('camera_mbytes', ts, (camera_total_m_bytes / 1048576), 'camera=' + camera_label + ' type=motion') | |
print_stat('camera_mbytes', ts, (camera_total_c_bytes / 1048576), 'camera=' + camera_label + ' type=capture') | |
def parse_timestamp(indate, intime): | |
m = re.match("(\d+)-*(\d+)*-*(\d+)*", intime) | |
hh = m.group(1) or '00' | |
mm = m.group(2) or '00' | |
ss = m.group(3) or '00' | |
longtime = "%s %s:%s:%s" % (indate, hh, mm, ss) | |
longtime = time.strptime(longtime, "%m-%d-%Y %H:%M:%S") | |
return time.mktime(longtime) | |
def main(): | |
#drop_privileges() | |
sys.stdin.close() | |
config_file = os.path.dirname(sys.argv[0]) + '/../etc/securityspy.json' | |
try: | |
config = json.load(open(config_file, 'r')) | |
except ValueError as e: | |
print >> sys.stderr, 'Config file (%s) json error: %s' % (config_file, e) | |
exit(1) | |
except: | |
print >> sys.stderr, 'Unable to load config file %s' % config_file | |
exit(1) | |
interval = 120 | |
while True: | |
ts = int(time.time()) | |
for dir in config['capture_dirs']: | |
do_dir(dir, ts) | |
sys.stdout.flush() | |
time.sleep(interval) | |
if __name__ == "__main__": | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment