Initial database consumer.
authorFrantišek Dvořák <valtri@civ.zcu.cz>
Wed, 4 Mar 2015 23:04:47 +0000 (00:04 +0100)
committerFrantišek Dvořák <valtri@civ.zcu.cz>
Wed, 4 Mar 2015 23:04:47 +0000 (00:04 +0100)
stat2pbs.py [new file with mode: 0755]

diff --git a/stat2pbs.py b/stat2pbs.py
new file mode 100755 (executable)
index 0000000..7986ffb
--- /dev/null
@@ -0,0 +1,94 @@
+#! /usr/bin/python2
+
+import getopt
+import sys
+import MySQLdb
+
+dbhost = 'localhost'
+dbname = 'hadoop'
+dbuser = 'hadoop'
+dbpassword = ''
+statfile = 'stat2pbs.txt'
+
+try:
+       opts, args = getopt.getopt(sys.argv[1:], 'hc:s:', ['help', 'config=', 'dbhost=', 'dbname=', 'dbuser=', 'dbpassword=', 'statfile=' ])
+except getopt.GetoptError:
+       print 'Args error'
+       sys.exit(2)
+for opt, arg in opts:
+       if opt in ('-h', '--help'):
+               print "stat2pbs.py [OPTIONS]\n\
+OPTIONS are:\n\
+  -h, --help ............ help message\n\
+  -c, --config .......... config file\n\
+  --dbhost\n\
+  --dbname\n\
+  --dbuser\n\
+  --dbpassword\n\
+  -s, --statfile FILE ... status file with the last timestamp"
+               sys.exit(0)
+       elif opt in ('-c', '--config'):
+               f = open(arg, 'r')
+               for line in f:
+                       cfg=line.rstrip().split('=')
+                       if cfg[0] == 'dbhost':
+                               dbhost = cfg[1]
+                       elif cfg[0] == 'dbname':
+                               dbname = cfg[1]
+                       elif cfg[0] == 'dbuser':
+                               dbuser = cfg[1]
+                       elif cfg[0] == 'dbpassword':
+                               dbpassword = cfg[1]
+                       elif cfg[0] == 'debug':
+                               debug = int(cfg[1])
+                       elif cfg[0] == 'statfile':
+                               statfile = int(cfg[1])
+               f.close()
+       elif opt in ('--dbhost'):
+               dbhost = arg
+       elif opt in ('--dbname'):
+               dbname = arg
+       elif opt in ('--dbuser'):
+               dbuser = arg
+       elif opt in ('--dbpassword'):
+               dbpassword = arg
+       elif opt in ('-s', '--statfile'):
+               statfile = arg
+       else:
+               print 'Args error'
+               sys.exit(2)
+
+try:
+       f = open(statfile, 'r')
+       ts = f.readline()
+       f.close()
+       ts = ts.rstrip()
+except IOError:
+       ts = None
+
+print 'Status file: %s' % statfile
+print 'Timestamp: %s' % ts
+
+db = MySQLdb.connect(dbhost, dbuser, dbpassword, dbname)
+st = db.cursor()
+
+# beware of actually running second - rather get only >5 seconds older changes
+if ts:
+       st.execute('SELECT id, name, user, changed FROM job WHERE changed > %s AND TIMESTAMPDIFF(SECOND,changed,NOW()) > 5 ORDER BY CHANGED', ts)
+else:
+       st.execute('SELECT id, name, user, changed FROM job WHERE TIMESTAMPDIFF(SECOND,changed,NOW()) > 5 ORDER BY changed')
+while 1:
+       data = st.fetchone()
+       if data:
+               print '%s, %s, %s' % (data[0], data[2], data[3])
+               ts = data[3]
+       else:
+               break
+
+db.close()
+
+if ts:
+       f = open(statfile, 'w')
+       f.write(str(ts) + '\n')
+       f.close()
+       print 'Written: %s' % ts