aff6349b194375c0cfc270b9c6a6c9fe6429bbde
[bwmon.git] / bwmon / util.py
blobaff6349b194375c0cfc270b9c6a6c9fe6429bbde
1 # -*- coding: utf-8 -*-
3 import curses
4 import sys
5 import ConfigParser
7 def clear():
8 """Clear the console using curses
10 This utility function empties the console ("clear").
11 """
12 curses.setupterm()
13 sys.stdout.write(curses.tigetstr("clear"))
14 sys.stdout.flush()
16 def read_monitor_config(configfile):
17 """Reads the monitor configuration file for the Aggregator
19 @param configfile: path of the config file
20 @return: a list of Monitor or PipeMonitor objects
21 """
22 config = ConfigParser.ConfigParser()
23 config.read(configfile)
24 for section in config.sections():
25 c = dict(config.items(section))
27 if c['type'] == 'monitor':
28 ignorelocal = parse_bool(c.get('ignorelocal', False))
29 import monitor
30 mon = monitor.Monitor(ignorelocal=ignorelocal)
31 inc = [c.get('include', '')]
32 exc = [c.get('exclude', '')]
33 mon.set_filter(inc, exc)
35 elif c['type'] == 'pipe':
36 import pipe
37 port = int(c['port'])
38 newhost = c['newhost']
39 newport = int(c['newport'])
41 mon = pipe.PipeMonitor(pipe.Pipe(port, newhost, newport))
42 #mon.set_shape(c.get('shape_threshold', 0))
44 else:
45 mon = None
46 print 'unknown monitor type %s' % c['type']
48 if mon:
49 yield mon
52 def parse_bool(val):
53 """Convert a string to a boolean value
55 @param val: The string value (or boolean)
56 @return: True or False, depending on "val"
57 """
58 if isinstance(val, bool):
59 return val
61 if string.lower() == 'true':
62 return True
64 return False
67 def read_notification_config(configfile):
68 """Reads the notification config file for the Aggregator
70 @param configfile: path to the config file
71 @return: a list of tuples representing the notification settings (process_regex, in_threshold, out_threshold, interval, command)
72 """
73 config = ConfigParser.ConfigParser()
74 config.read(configfile)
75 for section in config.sections():
76 c = dict(config.items(section))
77 yield ( c['process_filter'], int(c.get('in_threshold', 0)), int(c.get('out_threshold', 0)), int(c.get('interval', 1)), c.get('notification_command', '') )
80 class RingBuffer:
81 """A ringbuffer
82 """
84 def __init__(self,size_max):
85 """Initiates a new ringbuffer with the given size
87 @param size_max: maximum number of entries
88 """
89 self.max = size_max
90 self.data = []
92 def append(self,x):
93 """Append an element at the end of the buffer
95 @param x: The element to append
96 """
97 self.data.append(x)
98 if len(self.data) == self.max:
99 self.cur=0
100 self.__class__ = RingBufferFull
102 def get(self):
103 """Get a list of contained elements
105 @return: A list of ements from oldest to newesta
107 return self.data
110 class RingBufferFull:
111 """A full ringbuffer - not initialized directly
114 def __init__(self, n):
115 """Don't initialize this object directly!"""
116 raise "don't initialize FullRingBuffer directly"
118 def append(self,x):
119 """Append an element at the end of the buffer
121 @param x: The element to append
123 self.data[self.cur]=x
124 self.cur=(self.cur+1) % self.max
126 def get(self):
127 """Get a list of contained elements
129 @return: A list of ements from oldest to newesta
131 return self.data[self.cur:]+self.data[:self.cur]