pycollectd

Python interface to collectd-unixsock
git clone https://code.literati.org/pycollectd.git
Log | Files | Refs

collectdb.py (2615B)


      1 #!/usr/bin/python
      2 # Python built-in stuff
      3 import sqlite3, time
      4 
      5 
      6 class CollectDB(object):
      7     def __init__(self, filename=':memory:', names=[]):
      8         self._db = sqlite3.connect(filename)
      9         c = self._db.cursor()
     10         c.execute("""create table data (id integer primary key,
     11                                         timestamp integer not null, 
     12                                         hostname text not null,
     13                                         plugin text not null,
     14                                         plugin_instance text,
     15                                         type text not null,
     16                                         type_instance text)
     17                   """)
     18         c.execute('create index hostname on data (hostname)')
     19         c.execute('create index plugin on data (plugin)')
     20         c.execute('create index hostname_plugin on data (hostname, plugin)')
     21         c.execute('create index plugin_type on data (plugin, type)')
     22         c.execute('create unique index full on data (hostname, plugin, plugin_instance, type, type_instance)')
     23 
     24 
     25     def clear(self):
     26         c = self._db.cursor()
     27         c.execute('delete from data')
     28         self._db.commit()
     29 
     30     def add_values(self, values):
     31         c = self._db.cursor()
     32         c.executemany("""insert or replace into data (timestamp, 
     33                                                       hostname, 
     34                                                       plugin, 
     35                                                       plugin_instance, 
     36                                                       type, 
     37                                                       type_instance) 
     38                            values (?, ?, ?, ?, ?, ?)
     39                         """, ((time.mktime(timestamp.timetuple()),) + name.totuple() for timestamp, name in values))
     40         self._db.commit()
     41 
     42     def get_hostnames(self):
     43         c = self._db.cursor()
     44         c.execute("select distinct hostname from data")
     45         return [ r[0] for r in c ]
     46 
     47     def get_plugins(self, hostname=None):
     48         c = self._db.cursor()
     49         if hostname:
     50             where = ' where hostname=?'
     51             data = (hostname,)
     52         else:
     53             where = ''
     54             data = ()
     55 
     56         c.execute("select distinct plugin from data{0}".format(where), data)
     57         return [ r[0] for r in c ]
     58     
     59 
     60 def main():
     61     import collectd
     62     db = CollectDB()
     63     c = collectd.Collectd('/var/run/collectd-unixsock')
     64     db.add_values(c.listval())
     65     for hostname in db.get_hostnames():
     66         print hostname, 
     67         print db.get_plugins(hostname)
     68 
     69 
     70 if __name__ == '__main__':
     71     main()
     72