def AvailableSensors(self, cursor, range): """Return a HTML encoded list of the available sensors for a given day.""" global sensor_names sensors = [] cursor.execute( 'select distinct(sensor), max(epoch_seconds) from sensors ' 'where epoch_seconds > %d and ' 'epoch_seconds < %d group by sensor order by epoch_seconds;' % (range[0] - 1, range[1] + 1)) for row in cursor: t = datetime.datetime.utcfromtimestamp(row['max(epoch_seconds)']) sensors.append('<li><a href="/chart/%d.%d.%d/%s">%s</a> ' '(last reading at %s)' % (int(range[2][0]), int(range[2][1]), int( range[2][2]), row['sensor'], sensor_names.get(row['sensor'], row['sensor']), t)) for ent in os.listdir(PLUGIN_DIR): if ent.endswith('.py'): plugin = mplugin.LoadPlugin(PLUGIN_DIR, ent[:-3], log=self.log) try: for out in plugin.Returns(cursor): sensors.append( '<li><a href="/chart/%d.%d.%d/%s">%s</a>' % (int(range[2][0]), int( range[2][1]), int(range[2][2]), out, out)) except Exception, e: self.log( 'Exception determining return values for plugin %s: %s' % (ent, e))
def ResolveWouldReturn(self, cursor, sensor, start_epoch, end_epoch): """What would we get back if we resolved this sensor over this period?""" global sensor_names if sensor.startswith('='): plugin = mplugin.LoadPlugin(PLUGIN_DIR, sensor[1:], log=self.log) if not plugin: self.log('No plugin matching %s' % sensor[1:]) return values db = MySQLdb.connect(user=FLAGS.dbuser, db=FLAGS.dbname) cursor = db.cursor(MySQLdb.cursors.DictCursor) out = plugin.Returns(cursor) self.log('Resolving %s would return %s' % (sensor, repr(out))) return out else: sensors = [sensor] for s in sensor_names: if sensor_names[s] == sensor: sensors.append(s) hostnames = [] cursor.execute( 'select distinct(hostname) from sensors where ' 'sensor in ("%s") and epoch_seconds > %s and ' 'epoch_seconds < %s;' % ('", "'.join(sensors), start_epoch - 1, end_epoch + 1)) for row in cursor: hostnames.append(row['hostname']) if len(hostnames) == 1: self.log('Resolving %s would return %s' % (sensors, sensor)) return [sensor] out = [] for hostname in hostnames: out.append('%s (%s)' % (sensor, hostname)) self.log('Resolving %s would return %s' % (sensor, repr(out))) return out
lock.acquire() try: db = MySQLdb.connect(user = FLAGS.dbuser, db = FLAGS.dbname) cursor = db.cursor(MySQLdb.cursors.DictCursor) plugins = os.listdir(FLAGS.collector_dir) if FLAGS.only: plugins = [] for plugin in FLAGS.only.split(' '): plugins.append('%s.py' % plugin) for ent in plugins: if ent.endswith('.py'): print print '----------------------------------------------------------' print '%s: Running %s' %(datetime.datetime.now(), ent) try: plugin = mplugin.LoadPlugin(FLAGS.collector_dir, ent[:-3], log=None) plugin.Collect(cursor) except Exception, e: print '%s: Exception: %s' %(datetime.datetime.now(), e) finally: if FLAGS.lock: lock.release() if __name__ == "__main__": main(sys.argv)
def ResolveSensor(self, values, redirects, cursor, sensor, start_epoch, end_epoch, step_size, wideinterp): """Resolve a time series of this sensor value. This could be recursive.""" self.log('Values for %s between %s and %s' % (sensor, start_epoch, end_epoch)) plugin = None plugin_is_function = False if sensor.startswith('='): try: plugin = mplugin.LoadPlugin(PLUGIN_DIR, sensor[1:], log=self.log) except: pass if not plugin: try: plugin = mplugin.LoadPlugin(FUNCTION_DIR, sensor[1:].split('(')[0], log=self.log) except: pass plugin_is_function = True if not plugin: self.log('No plugin matching %s' % sensor[1:]) return values db = MySQLdb.connect(user=FLAGS.dbuser, db=FLAGS.dbname) cursor = db.cursor(MySQLdb.cursors.DictCursor) # Functions have explicit inputs if not plugin_is_function: inputs = plugin.Requires(cursor, sensor_names) else: inputs = sensor[1:].split('(')[1].split(')')[0].split(',') self.log('Calculation requires: %s' % repr(inputs)) # Fetch missing values for input in inputs: self.log('Plugin requires: %s' % input) if not input in values and input not in redirects: (values, redirects) = self.ResolveSensor( values, redirects, cursor, input, start_epoch, end_epoch, step_size, wideinterp) # Make redirect handling optional input_values = copy.deepcopy(values) for input in inputs: if input in redirects: input_values[input] = copy.deepcopy( self.HandleRedirects(values, redirects, input)) # Calculate self.log('Calculating derived values with inputs: %s' % input_values.keys()) if not plugin_is_function: values[sensor] = plugin.Calculate(input_values, redirects, step_size=step_size, log=self.log) else: values[sensor] = plugin.Calculate(inputs, input_values, redirects, step_size=step_size, log=self.log) else: sensors = self.ExpandSensorName(sensor) hostnames = [] cursor.execute( 'select distinct(hostname) from sensors where ' 'sensor in ("%s") and epoch_seconds > %s and ' 'epoch_seconds < %s;' % ('", "'.join(sensors), start_epoch - 1, end_epoch + 1)) for row in cursor: hostnames.append(row['hostname']) self.log('Found hostnames: %s' % repr(hostnames)) for hostname in hostnames: if len(hostnames) == 1: name = sensor else: name = '%s (%s)' % (sensor, hostname) redirects.setdefault(sensor, []) redirects[sensor].append(name) readings = self.GetData(cursor, sensors, hostname, start_epoch, end_epoch) values[name] = self.GetChartPoints(start_epoch, end_epoch, step_size, readings, sensor, wideinterp=wideinterp) self.log('Found %d chart points for %s' % (len(values[name]), name)) # self.log('Resolved values: %s and redirects: %s' %(repr(values), # repr(redirects))) return (values, redirects)
last_event = time.time() asyncore.loop(timeout=timeout, count=1) if time.time() - last_event > timeout: # We are idle print '%s ...' % datetime.datetime.now() db = MySQLdb.connect(user=FLAGS.dbuser, db=FLAGS.dbname) cursor = db.cursor(MySQLdb.cursors.DictCursor) for ent in os.listdir(DAILY_DIR): if ent.endswith('.py'): print '%s: Updating daily %s' % (datetime.datetime.now(), ent) try: plugin = mplugin.LoadPlugin(DAILY_DIR, ent[:-3], log=None) plugin.Daily(cursor) except Exception, e: print '%s: Exception: %s' % (datetime.datetime.now(), e) if time.time() - sensor_names_age > 1800: sensor_names = FetchSensorNames() InitializeCleanup() sensor_names_age = time.time() else: if Cleanup(): timeout = 0.2 else: