2 #Copyright (C) 2009-2010 :
3 # Gabes Jean, naparuba@gmail.com
4 # Gerhard Lausser, Gerhard.Lausser@consol.de
5 # Gregory Starck, g.starck@gmail.com
7 #This file is part of Shinken.
9 #Shinken is free software: you can redistribute it and/or modify
10 #it under the terms of the GNU Affero General Public License as published by
11 #the Free Software Foundation, either version 3 of the License, or
12 #(at your option) any later version.
14 #Shinken is distributed in the hope that it will be useful,
15 #but WITHOUT ANY WARRANTY; without even the implied warranty of
16 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 #GNU Affero General Public License for more details.
19 #You should have received a copy of the GNU Affero General Public License
20 #along with Shinken. If not, see <http://www.gnu.org/licenses/>.
25 #EXP_1=1/math.exp(5/60.0)
26 #EXP_5=1/math.exp(5/(5*60.0))
27 #EXP_15=1/math.exp(5/(15*60.0))
29 #From advance load average's code (another of my projects :) )
30 #def calc_load_load(load, exp,n):
31 # load = n + exp*(load - n)
35 #This class if for having a easy Load calculation
36 #without having to send value at regular interval
37 #(but it's more efficient if you do this :) ) and not
38 #having a list and all. Just an object, an update and a get
39 #You can define m : the average is for m minutes. The val is
40 #the initial value. It's better if it's 0 but you can choice.
44 #m = number of minutes for the average
46 def __init__(self
, m
=1, initial_value
=0):
47 self
.exp
= 0 #first exp
48 self
.m
= m
#Number of minute of the avg
49 self
.last_update
= 0 #last update of the value
50 self
.val
= initial_value
#first value
53 def update_load(self
, new_val
):
54 #The first call do not change the value, just tag
55 #the begining of last_update
56 if self
.last_update
== 0:
57 self
.last_update
= time
.time()
61 diff
= now
- self
.last_update
62 self
.exp
= 1/math
.exp(diff
/ (self
.m
*60.0))
63 self
.val
= new_val
+ self
.exp
*(self
.val
- new_val
)
64 self
.last_update
= now
65 except OverflowError: #if the time change without notice, we overflow :(
67 except ZeroDivisionError: #do not care
75 if __name__
== '__main__':
78 for i
in xrange(1, 300):
80 print '[', int(time
.time() - t
), ']', l
.get_load(), l
.exp