Example yaml
[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                 self.props = {}
11
12                 self.matches = {}
13                 for e in self.parsed:
14                         e['compiled_regex'] = re.compile(r'%s'%e['regex'])
15                         self.props[e['name']] = e
16                         self.matches[e['name']]={}
17         
18         def match_on_file(self, f):
19                 for e in self.parsed:
20                         r = e['compiled_regex']
21                         m = re.match(r, f)
22                         if m:
23                                 key_path = [e['name']] + list(m.groups())
24                                 key = '###'.join(key_path)
25                                 try:
26                                         self.matches[e['name']][key].append(f)
27                                 except KeyError:
28                                         self.matches[e['name']][key]=[f]
29
30         def match_on_directory(self, dirname):
31                 cmdline = '%s %s -printf "%%P\n"'%(FIND_PATH, dirname)
32                 files = [line.rstrip() for line in os.popen(cmdline)]
33
34                 for f in files:
35                         self.match_on_file(f)
36
37         def gen_defs(self):
38                 i0 = 0
39                 i1 = 0
40                 self.defs = []
41                 self.cdefs = []
42                 self.xports = []
43                 for e in self.parsed:
44                         n = e['name']
45                         psexp = []
46                         
47                         for k,l in self.matches[n].items():
48                                 xvar='x%d'%i1
49                                 for v in l:
50                                         svar='s%d'%i0
51                                         ds = self.props[n]['ds']
52                                         try:
53                                                 print self.props[n]['step']     
54                                         except:
55                                                 pass
56
57                                         try:
58                                                 step = ':step=%r'%self.props[n]['step']
59                                         except:
60                                                 step = ''
61                                 
62                                         try:
63                                                 cf = self.props[n]['cf']
64                                         except:
65                                                 cf = 'AVERAGE'
66
67                                         self.defs.append('DEF:%s=%s:%s:%s%s'%(svar,v,ds,cf,step))
68                                         if (psexp):
69                                                 psexp.extend([svar,'ADDNAN'])
70                                         else:
71                                                 psexp=[svar]
72                                         i0+=1
73
74                                 self.cdefs.append('CDEF:%s=%s'%(xvar,','.join(psexp)))
75                                 i1+=1
76                                 self.xports.append('XPORT:%s:"%s"'%(xvar,k))