1
+ require 'rubygems'
2
+ require 'gchart'
3
+
4
+ def get_hosts
5
+ # probly want to do this at init
6
+
7
+ paths = Dir [ CONF [ 'rrd_data_home' ] +"/*" ] . sort
8
+ #hosts = {}
9
+ hosts = [ "All" ]
10
+ #count = 0
11
+ for path in paths
12
+ p = path . split ( "/" )
13
+ #hosts[p[-1]] = count
14
+ #count = count +1
15
+ hosts . push ( p [ -1 ] )
16
+ puts "hosts found: #{ p [ -1 ] } "
17
+ end
18
+
19
+ hosts
20
+ end
21
+ def get_rrdtypes
22
+ puts "Searching for RRD Types"
23
+ paths = Dir [ CONF [ 'rrd_data_home' ] +"/**/*.rrd" ] . sort
24
+ rrdtypes = [ "All" ]
25
+ dir_exclude = CONF [ 'dir_exclude' ] . split ( " " )
26
+ for path in paths
27
+ p = path . split ( "/" )
28
+ if ( rrdtypes . include? ( p [ -1 ] ) )
29
+ print "."
30
+ else
31
+ rrdtypes . push ( p [ -1 ] ) unless dir_exclude . include? ( p [ -2 ] )
32
+ print "*"
33
+ end
34
+ end
35
+ rrdtypes
36
+ end
37
+
38
+ def median ( array , already_sorted = false )
39
+ return nil if array . empty?
40
+ array = array . sort unless already_sorted
41
+ m_pos = array . size / 2
42
+ return array . size % 2 == 1 ? array [ m_pos ] : mean ( array [ m_pos -1 ..m_pos ] )
43
+ end
44
+ def mean ( array )
45
+ array . inject ( 0 ) { |sum , x | sum +=x } /array . size . to_f
46
+ end
47
+
48
+ def get_rrd ( interval , rrd_file , rrd_type , resolution )
49
+ time = Time . now
50
+ start_time = ( ( time . tv_sec /resolution . to_i ) *resolution . to_i )
51
+ puts "#{ CONF [ 'rrdtool_path' ] } /rrdtool fetch #{ rrd_file } #{ rrd_type } -e #{ start_time } -r #{ resolution } -s e-#{ interval } m |#{ CONF [ 'awk_path' ] } /awk '{printf\" %s,\" , $2}'"
52
+ rawdata = `#{ CONF [ 'rrdtool_path' ] } /rrdtool fetch #{ rrd_file } #{ rrd_type } -e #{ start_time } -r #{ resolution } -s e-#{ interval } m |#{ CONF [ 'awk_path' ] } /awk '{printf"%s,", $2}'`
53
+ datas = rawdata . split ( "," )
54
+ mdata = [ ]
55
+ ymax = 0
56
+ ymin = 100000000
57
+ for d in datas
58
+ #STDOUT.print "#{d} " unless d.downcase =~ /nan/
59
+ stat = d . to_f
60
+ if ( stat )
61
+ mdata . push ( stat ) unless d . downcase =~ /nan/
62
+ end
63
+ if ( ymax < stat )
64
+ ymax = d . to_f
65
+ end
66
+ if ( ymin > stat )
67
+ ymin = d . to_f
68
+ end
69
+ end
70
+ is_zero = false
71
+ stat_median = median ( mdata )
72
+ if ( stat_median == 0 and ymin . to_f == 0 && ymax . to_f == 0 )
73
+ is_zero = true
74
+ end
75
+ [ mdata , is_zero , ymin , ymax , stat_median ]
76
+ end
77
+
78
+
79
+ def get_uri ( rrd_file , interval , resolution , host )
80
+
81
+ # check exclude list
82
+ #
83
+
84
+ names = rrd_file . split ( "\/ " )
85
+ source = "http://chart.apis.google.com/chart"
86
+ type = "ls"
87
+ colour = "8198A2"
88
+ threshold_colour = "ff0000"
89
+
90
+ is_zero = false
91
+ min_data , is_zero , ymin , ymax , stat_median = get_rrd ( interval , rrd_file , "MIN" , resolution )
92
+ max_data , is_zero , ymin , ymax , stat_median = get_rrd ( interval , rrd_file , "MAX" , resolution )
93
+ avg_data , is_zero , ymin , ymax , stat_median = get_rrd ( interval , rrd_file , "AVERAGE" , resolution )
94
+
95
+
96
+ # 80th percentile stuff, not needed right now
97
+
98
+
99
+ all_data = [ ]
100
+
101
+ if ( min_data == avg_data && max_data == avg_data )
102
+ all_data = avg_data
103
+ else
104
+ all_data = [ min_data , max_data , avg_data ]
105
+ end
106
+ # :line_colors => 'FF0000,00FF00,666666',
107
+ uri = Gchart . line ( :size => '1000x165' ,
108
+ :title => "#{ host } #{ names [ -1 ] } " ,
109
+ :bg => 'FFCCFF' ,
110
+ :line_colors => 'FF0000,00FF00,666666' ,
111
+ :axis_with_labels => [ 'x' , 'y' , 'r' ] ,
112
+ :encoding => 'simple' ,
113
+ :data => all_data )
114
+
115
+ names = rrd_file . split ( "\/ " )
116
+ [ uri , names [ -1 ] , ymin , ymax , stat_median , is_zero ]
117
+ end
118
+
119
+ def draw_table ( img_src , counter_name , ymin , ymax , stat_median , dir_name )
120
+ template = ERB . new <<-EOF
121
+ <center>
122
+ <table id=result border=0><tr>
123
+ <td id=counter><%= counter_name %></td>
124
+ <td rowspan=2 id=sparkline>
125
+ <img src=<%=img_src%>></a></td>
126
+ <td rowspan=2>
127
+ <div id=max><%= ymax.to_f %></div>
128
+ <div id=med><%= stat_median %></div>
129
+ <div id=min><%= ymin.to_f %></div></td><td rowspan=2>put my description here</td>
130
+ </tr>
131
+ <tr>
132
+ <td id=filter><%= dir_name %></td>
133
+ </tr>
134
+ </table></center>
135
+
136
+ EOF
137
+ template . result ( binding )
138
+
139
+
140
+ end
0 commit comments