def main(config): graphite_server = config.get("graphite", "server") graphite_prefix = config.get("graphite", "prefix") print "Initializing graphiteudp, server: {}, prefix: {}".format(graphite_server, graphite_prefix) graphiteudp.init(graphite_server, prefix=graphite_prefix) dbfile = os.path.join(os.environ.get('HOME'), 'scans.sqlite3') dbfile = 'scans.sqlite3' need_schema = False if not os.path.exists(dbfile): need_schema = True db = sqlite3.connect(dbfile) if need_schema: db.execute('create table scans(barcode text, timestamp datetime, event_id text)') dev = [InputDevice(device) for device in list_devices() if InputDevice(device).name == scanner_name][0] def signal_handler(signal, frame): print 'Stopping' dev.ungrab() sys.exit(0) signal.signal(signal.SIGINT, signal_handler) dev.grab() barcode = "" last_event_time = 0 last_event_id = None for event in dev.read_loop(): if event.type == ecodes.EV_KEY: data = categorize(event) # Catch only keydown, and not Enter if data.keystate == 1 and data.scancode != 42: if data.scancode == 28: timestamp = time.time() if timestamp - last_event_time < 10: event_id = last_event_id else: event_id = uuid.uuid1() last_event_id = event_id last_event_time = timestamp graphiteudp.send('event.scan', 1) db.execute( 'insert into scans (barcode, timestamp, event_id) ' + 'values (:barcode, :timestamp, :event_id)', [barcode, timestamp, event_id.get_urn()]) db.commit() barcode = "" else: try: barcode += scancodes[data.scancode] except KeyError: print >>sys.stderr, "Unknown scancode: {0}".format(data.scancode)
def init_graphite(server, prefix): """Initializes the graphite settings :param server: The graphite server to connect to :param prefix: The prefix to prepend to sent data """ print "Initializing graphiteudp, server: {}, prefix: {}".format(server, prefix) graphiteudp.init(server, prefix=prefix)
def __init__( self, state_filename, log_filename, log_fields, graphite_host, graphite_prefix): self.state_filename = state_filename self.log_filename = log_filename self.log_fields = log_fields self.graphite_host = graphite_host self.graphite_prefix = graphite_prefix if len(sys.argv) > 1 and sys.argv[1] == '-': self.test_mode = True logging.basicConfig(level=logging.DEBUG) else: self.test_mode = False graphiteudp.init(host=self.graphite_host, prefix=self.graphite_prefix, debug=self.test_mode)
import sys import logging sys.path.insert(0, "../src/") import graphiteudp graphiteudp.send("foo", 1.2) logger = logging.getLogger() logger.setLevel(logging.DEBUG) handler = logging.StreamHandler() # Writes to stderr by default handler.setFormatter(logging.Formatter('%(process)d %(name)s %(levelname)s %(message)s')) logger.addHandler(handler) graphiteudp.init(debug = True) graphiteudp.send("foo2", 1.2) graphiteudp.init("example.com") graphiteudp.send("foo3", 1.2) graphiteudp.init("example.com", 2005) graphiteudp.send("foo3", 1.2) graphiteudp.init("example.com", 2005, prefix = "bzrt") graphiteudp.send("foo4", 1.2, 100)
import sys import logging sys.path.insert(0, "../src/") import graphiteudp graphiteudp.send("foo", 1.2) logger = logging.getLogger() logger.setLevel(logging.DEBUG) handler = logging.StreamHandler() # Writes to stderr by default handler.setFormatter( logging.Formatter('%(process)d %(name)s %(levelname)s %(message)s')) logger.addHandler(handler) graphiteudp.init(debug=True) graphiteudp.send("foo2", 1.2) graphiteudp.init("example.com") graphiteudp.send("foo3", 1.2) graphiteudp.init("example.com", 2005) graphiteudp.send("foo3", 1.2) graphiteudp.init("example.com", 2005, prefix="bzrt") graphiteudp.send("foo4", 1.2, 100)
logging.debug("Initializing fan control") fan = fan_control(cfg.gpio_pin) logging.debug("Logging in to Nest thermostat") n = Nest(cfg.nest_username, cfg.nest_password) try: n.login() except urllib2.URLError: logging.error("Unable to login to Nest") sys.exit(1) #print n.status['device'][n.serial] #print n.status['shared'][n.serial] print cfg.graphite_prefix.lower() graphiteudp.init(cfg.graphite_host, prefix = cfg.graphite_prefix.lower()) bus = smbus.SMBus(cfg.smbus) while True: try: n.get_status() except urllib2.URLError: pass except: logging.error("Unexpected error. Continuing with last values from Nest.") away = n.status['shared'][n.serial]['auto_away'] hvac_fan_on = n.status['shared'][n.serial]['hvac_fan_state'] while True: try:
except: redis = None # For librato support try: import librato from librato.metrics import Gauge as LibratoGauge from librato.metrics import Counter as LibratoCounter except ImportError: librato = None # For graphite support try: import graphiteudp graphiteudp.init( getattr(settings, 'APP_METRICS_GRAPHITE_HOST', '127.0.0.1'), prefix=getattr(settings, 'APP_METRICS_GRAPHITE_APIKEY', '')) except ImportError: graphiteudp = None class MixPanelTrackError(Exception): pass # DB Tasks @task def db_metric_task(slug, num=1, **kwargs): met = Metric.objects.get(slug=slug) MetricItem.objects.create(metric=met, num=num)
except: redis = None # For librato support try: import librato from librato.metrics import Gauge as LibratoGauge from librato.metrics import Counter as LibratoCounter except ImportError: librato = None # For graphite support try: import graphiteudp graphiteudp.init(getattr(settings, 'APP_METRICS_GRAPHITE_HOST', '127.0.0.1'), prefix=getattr(settings, 'APP_METRICS_GRAPHITE_APIKEY', '')) except ImportError: graphiteudp = None class MixPanelTrackError(Exception): pass # DB Tasks @task def db_metric_task(slug, num=1, **kwargs): met = Metric.objects.get(slug=slug)
logging.debug("Initializing fan control") fan = fan_control(cfg.gpio_pin) logging.debug("Logging in to Nest thermostat") n = Nest(cfg.nest_username, cfg.nest_password) try: n.login() except urllib2.URLError: logging.error("Unable to login to Nest") sys.exit(1) #print n.status['device'][n.serial] #print n.status['shared'][n.serial] print cfg.graphite_prefix.lower() graphiteudp.init(cfg.graphite_host, prefix=cfg.graphite_prefix.lower()) bus = smbus.SMBus(cfg.smbus) while True: try: n.get_status() except urllib2.URLError: pass except: logging.error( "Unexpected error. Continuing with last values from Nest.") away = n.status['shared'][n.serial]['auto_away'] hvac_fan_on = n.status['shared'][n.serial]['hvac_fan_state'] while True: