Bug fixes
[rrdaggregate.git] / aggmgr.py
1 import yaml
2 import os
3 import re
4
5 FIND_PATH='find'
6
7 class AggManager:
8         def __init__(self, program):
9                 self.parsed = yaml.load(open(program))['entries']
10
11                 self.matches = {}
12                 for e in self.parsed:
13                         e['compiled_regex'] = re.compile(r'%s'%e['regex'])
14                         self.matches[e['name']]={}
15         
16         def match_on_file(self, f):
17                 for e in self.parsed:
18                         r = e['compiled_regex']
19                         m = re.match(r, f)
20                         if m:
21                                 key = '###'.join(m.groups())
22                                 try:
23                                         self.matches[e['name']][key].append(f)
24                                 except KeyError:
25                                         self.matches[e['name']][key]=[f]
26
27         def match_on_directory(self, dirname):
28                 cmdline = '%s %s -printf "%%P\n"'%(FIND_PATH, dirname)
29                 files = [line.rstrip() for line in os.popen(cmdline)]
30
31                 for f in files:
32                         self.match_on_file(f)
33
34         def gen_defs(self):
35                 for e in self.parsed:
36                         n = e['name']
37                         i0 = 0
38                         i1 = 0
39                         self.defs = []
40                         self.cdefs = []
41                         self.xports = []
42                         psexp = []
43                         for k,l in self.matches[n].items():
44                                 xvar='x%d'%i1
45                                 for v in l:
46                                         svar='s%d'%i0
47                                         self.defs.append('DEF:%s=%s:value:AVERAGE'%(svar,v))
48                                         if (psexp):
49                                                 psexp.extend([svar,'+'])
50                                         else:
51                                                 psexp=[svar]
52                                         i0+=1
53
54                                 self.cdefs.append('CDEF:%s=%s'%(xvar,','.join(psexp)))
55                                 i1+=1
56                                 self.xports.append('XPORT:%s:"%s"'%(xvar,k))
57                                 
58                         
59
60
61
62