1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | ''' |
---|
4 | log EPICS data into RRD files |
---|
5 | ''' |
---|
6 | |
---|
7 | ########### SVN repository information ################### |
---|
8 | # $Date: 2011-01-19 01:27:35 +0000 (Wed, 19 Jan 2011) $ |
---|
9 | # $Author: jemian $ |
---|
10 | # $Revision: 236 $ |
---|
11 | # $HeadURL$ |
---|
12 | # $Id: epics2rrd.py 236 2011-01-19 01:27:35Z jemian $ |
---|
13 | ########### SVN repository information ################### |
---|
14 | |
---|
15 | #------------------------------------------------------------- |
---|
16 | |
---|
17 | import os |
---|
18 | import time |
---|
19 | import sys |
---|
20 | import rrd_pv # coordination between EPICS and RRD |
---|
21 | import xmlSupport # read pvlist.xml and properties.xml files |
---|
22 | |
---|
23 | #------------------------------------------------------------- |
---|
24 | |
---|
25 | svnrev = "$Revision: 236 $".strip("$").split()[1] |
---|
26 | |
---|
27 | message = time.ctime() |
---|
28 | message += " %s (r%s)" % ( sys.argv[0], svnrev ) |
---|
29 | message += " PID=" + repr(os.getpid()) |
---|
30 | message += " starting on HOST=" + os.environ['HOST'] |
---|
31 | message += " by user=" + os.environ['USER'] |
---|
32 | print "# ", message |
---|
33 | sys.stdout.flush() |
---|
34 | |
---|
35 | db = {} |
---|
36 | cfg = xmlSupport.readConfigurationXML('/home/joule/WEB33/www/rrd') |
---|
37 | properties = cfg['properties'] |
---|
38 | |
---|
39 | pvList = cfg['pvList'] |
---|
40 | |
---|
41 | for pvEntry in pvList: |
---|
42 | name = pvEntry['pv'] |
---|
43 | if not name in db.keys(): |
---|
44 | basedir = properties['rrd_logs_base_dir'] |
---|
45 | rrdpath = properties['subdirs']['rrd_file_dir'] |
---|
46 | rrdfile = pvEntry['fileRoot'] + '.rrd' |
---|
47 | rrd_database = os.path.join(basedir, rrdpath, rrdfile) |
---|
48 | |
---|
49 | fileRoot = pvEntry['fileRoot'] |
---|
50 | pvObject = rrd_pv.rrd_pv(name, rrd_database) |
---|
51 | db[name] = pvObject |
---|
52 | |
---|
53 | # throttle the printing of messages in the main event loop |
---|
54 | nexttime = time.time() |
---|
55 | update_interval_seconds = 10*60 # update every 10 minutes |
---|
56 | |
---|
57 | while 1: |
---|
58 | pvcount = 0 |
---|
59 | keys = db.keys() |
---|
60 | keys.sort() |
---|
61 | notconnected_msgs = [] |
---|
62 | for name in keys: |
---|
63 | rrdPv = db[name] |
---|
64 | if rrdPv.pv.connected: |
---|
65 | pvcount += 1 |
---|
66 | if rrdPv.pv.value == None: |
---|
67 | rrdPv.pv.get() |
---|
68 | rrdPv.update() |
---|
69 | #print rrdPv |
---|
70 | else: |
---|
71 | notconnected_msgs.append("Trying to connect: %s" % name) |
---|
72 | rrd_database = rrdPv.rrd |
---|
73 | #rrdPv.pv.ch.connect() # does not seem to connect if initial try failed |
---|
74 | # replace existing object |
---|
75 | db[name] = rrd_pv.rrd_pv(name, rrd_database) |
---|
76 | if (time.time() >= nexttime): |
---|
77 | # throttle the frequency of this message: |
---|
78 | if len(notconnected_msgs): |
---|
79 | for msg in notconnected_msgs: |
---|
80 | print "# ", msg |
---|
81 | print "# ", time.ctime()," PID=" + repr(os.getpid()), |
---|
82 | print os.environ['USER'] + '@' + os.environ['HOST'], |
---|
83 | print " %d of %d PVs connected" % (pvcount, len(db)) |
---|
84 | nexttime = time.time() + update_interval_seconds # set next update time |
---|
85 | sys.stdout.flush() |
---|
86 | time.sleep(5) |
---|