root/trunk/tool/metroSite.php @ 73

Revision 68, 10.1 KB (checked in by andreu, 15 years ago)

add script to graph client stats from many prefixes

Line 
1#!/usr/bin/php
2<?php
3    function generation_graph_jour($nom_du_site, $prefixeTab) {
4        // SAGA
5        global $cfgTmp, $cfgFileSep, $cfgRrdPath;
6
7        $png_path = "/var/www/saga/www/images/tmp/";
8        $nfs_path = $cfgRrdPath.$cfgFileSep;
9        // SAGA
10
11        // FX
12        //$png_path = "/tmp/";
13        //$nfs_path = "/mnt/data/RRD/RENATER/IPv4_SUBNET/";
14        // FX
15        $nbpref = count($prefixeTab);
16        $found = 0;
17        $nofoundnb = 0;
18        $foundone = 0;
19        $DEF_string = "";
20        $CDEF_string = "";
21        $CDEF_str_ALL = "";
22        $all = "";
23        $AREA_string = "";
24        $CDEF_str_ALLIN = "";
25        $CDEF_str_ALLOUT = "";
26        $all = "";
27        $allout = "";       
28
29        foreach($prefixeTab as $key => $value) {
30
31                # creation du nom de fichier .rrd correspondant (du type ".../193_49_159_0_24_X.rrd")
32        # X n'est pas connu, c'est la valeur d'echantillonnage
33        # ce fichier sera sur le serveur de donnees d ou $nfs_path
34          $tmp = str_replace(".", "_", $value[0]);
35          $prefix_name = $tmp . "_" . $value[1];
36          if (is_dir($nfs_path)) {
37            if ($dh = opendir($nfs_path)) {
38              while (($file = readdir($dh)) !== false) {
39                if (strpos($file, $prefix_name) !== false) {
40              $found = 1;
41              break;
42            }
43              }
44              if ($found != 1){
45                $found = 0;
46            $nofoundnb+=1;
47              }
48              closedir($dh);
49            }
50          }
51          $rrdname = $nfs_path.$file;
52          $file_split = split("_", $file);
53          $tmp2 = split("\.",$file_split[5]);
54          $sampling = $tmp2[0];
55
56          if ($found==1){
57            $foundone=1;
58            $input = "inBytesG".$key;
59            $output = "outBytesG".$key;
60            $rrdfiles = "DEF:$input=".$rrdname.":inBytes:AVERAGE"." "."DEF:$output=".$rrdname.":outBytes:AVERAGE";
61            if ( $DEF_string == "") {
62              $DEF_string = "$rrdfiles"."";
63            } else {
64              $DEF_string = "$rrdfiles"." ".$DEF_string;
65            }
66           
67            $input10 = "input10".$key;
68            $output10 = "output10".$key;
69            $strtmp = "CDEF:$input10=$input,8,*,5,/,60,/,$sampling,*"." "."CDEF:$output10=$output,8,*,5,/,60,/,$sampling,*";
70            if ( $CDEF_string == "") {
71              $CDEF_string = "$strtmp"."";
72            } else {
73              $CDEF_string = "$strtmp"." ".$CDEF_string;
74            }
75            $tmpin = "$input,UN,1,$input,IF,8,*,5,/,60,/,$sampling,*";
76            if ( $all == "") {
77              $all = "$tmpin"."";
78            } else {
79              $all = "$tmpin".",".$all;
80            }
81            $tmpout = "$output,UN,1,$output,IF,8,*,5,/,60,/,$sampling,*";
82            if ( $allout == "") {
83              $allout = "$tmpout"."";
84            } else {
85              $allout = "$tmpout".",".$allout;
86            }
87          }
88          $found = 0;
89        }
90        for ($i=0; $i < $nbpref-$nofoundnb-1; $i++){
91          $all = $all.",+";
92          $allout = $allout.",+";
93        }
94        $CDEF_str_ALLIN = "CDEF:TotalIN=".$all;
95        $CDEF_str_ALLOUT = "CDEF:TotalOUT=".$allout;
96# creation du nom de fichier de l'image a creer
97        if ($foundone==1){
98          $filename = $nom_du_site."_j".".png";
99          $pngname = $png_path.$filename;
100         
101# creation de l'image dans $png_path.
102          $res = `rrdtool graph $pngname -s -1D --title "$nom_du_site" --vertical-label "bits/s" -A $DEF_string $CDEF_string $CDEF_str_ALLIN $CDEF_str_ALLOUT AREA:TotalIN\#00FF00:TotalIN LINE:TotalOUT\#0000FF:TotalOUT GPRINT:TotalIN:MAX:"MAX In %1.1lf%S b/s" GPRINT:TotalOUT:MAX:"MAX Out %1.1lf%S b/s"`;
103         
104        # todo FX : faire un retour different si erreur
105          return $filename;
106        }
107}
108
109    function generation_graph_mois($nom_du_site, $prefixeTab) {
110        // SAGA
111        global $cfgTmp, $cfgFileSep;
112
113        $png_path = "/var/www/saga/www/images/tmp/";
114        $tmp_path .= $cfgTmp.$cfgFileSep;
115        // SAGA
116
117        // FX
118        //$png_path = "/tmp/";
119        //$tmp_path = "/tmp/";
120        //$nfs_path = "/mnt/data/RRD/RENATER/IPv4_SUBNET/";
121        // FX
122        $nbpref = count($prefixeTab);
123        $found = 0;
124        $nofoundnb = 0;
125        $foundone = 0;
126        $DEF_string = "";
127        $CDEF_string = "";
128        $CDEF_str_ALL = "";
129        $all = "";
130        $AREA_string = "";
131        $CDEF_str_ALLIN = "";
132        $CDEF_str_ALLOUT = "";
133        $all = "";
134        $allout = "";             
135       
136        # creation du nom de fichier de l'image a creer
137        $filename = $nom_du_site."_m".".png";
138        $pngname = $png_path.$filename;
139       
140        $tab_in = array();
141        $tab_out = array();
142        foreach($prefixeTab as $key => $value) {
143          $found = 0;
144                # creation du nom de fichier .rrd correspondant (du type ".../193_49_159_0_24_X.rrd")
145        # X n'est pas connu, c'est la valeur d'echantillonnage
146        # ce fichier sera sur le serveur de donnees d ou $nfs_path
147          $tmp = str_replace(".", "_", $value[0]);
148          $prefix_name = $tmp . "_" . $value[1];
149          if (is_dir($nfs_path)) {
150            if ($dh = opendir($nfs_path)) {
151            while (($file = readdir($dh)) !== false) {
152              if (strpos($file, $prefix_name) !== false) {
153            $found = 1;
154            break;
155              }
156            }
157            if ($found != 1){
158              $found = 0;
159              $nofoundnb+=1;
160            }
161            closedir($dh);
162            }
163          }
164          if ($found==1){
165            $rrdname = $nfs_path.$file;
166            $file_split = split("_", $file);
167            $tmp2 = split("\.",$file_split[5]);
168            $sampling = $tmp2[0];
169# recuperation des donnees par un rrd fecht pour calculer le debit 95%percentile
170            $tmp_file = $tmp_path.$nom_du_site.".tmp";
171            `rrdtool fetch $rrdname AVERAGE --start -31Days | cut -d " " -f 4,5 > $tmp_file`;
172            $fd = fopen ($tmp_file, "r");
173            $i=0;
174            while (!feof ($fd)) {
175              $buffer = fgets($fd, 4096);
176              $buffer_split = split(" ",$buffer);
177              if (trim($buffer_split[0]) != "nan"){
178            $tab_in[$i] += trim($buffer_split[0]);
179              }
180              if (trim($buffer_split[1]) != "nan"){
181            $tab_out[$i] += trim($buffer_split[1]);
182              }
183              $i++;
184            }
185            fclose ($fd);
186            `rm $tmp_file`;
187            $foundone=1;
188            $input = "inBytesG".$key;
189            $output = "outBytesG".$key;
190            $rrdfiles = "DEF:$input=".$rrdname.":inBytes:AVERAGE"." "."DEF:$output=".$rrdname.":outBytes:AVERAGE";
191            if ( $DEF_string == "") {
192              $DEF_string = "$rrdfiles"."";
193            } else {
194              $DEF_string = "$rrdfiles"." ".$DEF_string;
195            }
196           
197            $input10 = "input10".$key;
198            $output10 = "output10".$key;
199            $strtmp = "CDEF:$input10=$input,8,*,5,/,60,/,$sampling,*"." "."CDEF:$output10=$output,8,*,5,/,60,/,$sampling,*";
200            if ( $CDEF_string == "") {
201              $CDEF_string = "$strtmp"."";
202            } else {
203              $CDEF_string = "$strtmp"." ".$CDEF_string;
204            }
205            $tmpin = "$input,UN,1,$input,IF,8,*,5,/,60,/,$sampling,*";
206            if ( $all == "") {
207              $all = "$tmpin"."";
208            } else {
209              $all = "$tmpin".",".$all;
210            }
211            $tmpout = "$output,UN,1,$output,IF,8,*,5,/,60,/,$sampling,*";
212            if ( $allout == "") {
213              $allout = "$tmpout"."";
214            } else {
215              $allout = "$tmpout".",".$allout;
216            }
217            $found = 0;
218          }
219        }
220        if ($foundone==1){
221          for ($i=0; $i < $nbpref-$nofoundnb-1; $i++){
222            $all = $all.",+";
223            $allout = $allout.",+";
224          }
225          $CDEF_str_ALLIN = "CDEF:TotalIN=".$all;
226          $CDEF_str_ALLOUT = "CDEF:TotalOUT=".$allout;
227          rsort($tab_in,SORT_NUMERIC);
228          rsort($tab_out,SORT_NUMERIC);
229          foreach ( $tab_in as $key => $value ) {
230            if ($value != "nan") {$lgin++;}
231          }
232          $percin = $lgin-intval(($lgin*95/100));
233          foreach ( $tab_out as $key2 => $value2 ) {
234            if ($value2 != "nan") {$lgout++;}
235          }
236          $percout = $lgout-intval(($lgout*95/100));
237          $dr1 max($tab_in[$percin-1],$tab_out[$percout-1]);
238          $debit = number_format($dr1*$sampling*8/5/60,0,",","");
239                  $debit = number_format($dr1*$sampling*8/5/60,0,",","");
240                  if ($debit > 1000000000) {
241                        $debit2 = $debit/1000000000;
242                        $Scale = "G";
243                  }else if ($debit > 1000000) {
244                        $debit2 = $debit/1000000;
245                        $Scale = "M";
246                  }else if ($debit > 1000) {
247                        $debit2 = $debit/1000;
248                        $Scale = "K";
249                  }else{
250                        $Scale = "";
251                  }
252                  $debit2 = number_format($debit2,2,",","");
253# creation de l'image dans $png_path.
254          $res = `rrdtool graph $pngname -s -31D --title "$nom_du_site" --vertical-label "bits/s" -A $DEF_string $CDEF_string $CDEF_str_ALLIN $CDEF_str_ALLOUT AREA:TotalIN\#00FF00:TotalIN LINE:TotalOUT\#0000FF:TotalOUT GPRINT:TotalIN:MAX:"MAX In %1.1lf%S b/s" GPRINT:TotalOUT:MAX:"MAX Out %1.1lf%S b/s" HRULE:$debit\#FF0000:"RENATER debit (95th percentile) $debit2 $Scale bits/s"`;
255       
256# todo FX : faire un retour different si erreur
257          return $filename;
258        }
259    }
260
261    function graphiqueMetrologie() {
262        // SAGA
263
264        $monAg = new Agrement_2($this->tuple['ip_ag_numero'],$this->tuple['ip_ag_version'], $this->dbh);
265        $monAg->querySelect('r', true);
266        $nom_du_site = $monAg->get_dc_et_code();
267        $prefixe = $this->tuple['ip_adresse'];
268        $cidr = $this->tuple['ip_cidr'];
269        // SAGA
270
271        // FX
272        //$png_path = "/tmp/";
273        //$nfs_path = "/mnt/data/RRD/RENATER/IPv4_SUBNET/"; # expl: "/mnt/db/RENATER_PREFIXES_RRD/";
274        //$nom_du_site = "U-BS-VANNES";
275        //$prefixe = "193.54.227.0";
276        //$cidr = "24";
277        // FX
278       
279        // SAGA
280        $title = "Graphiques Métrologie";
281        require_once("saga_2.inc.php");
282        include("head_2.inc.php");
283        // SAGA
284
285        // FX
286        //$html = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">";
287        //$html = "<html>";
288        //$html = "<head>";
289        //$html = "<title>Graphiques Métrologie</title>";
290        //$html = "<body>\n";
291        // FX
292
293        if ($found == true) {
294
295            $pngJour = $this->generation_graph_jour($nom_du_site, $prefixeTab);
296            $pngMois = $this->generation_graph_mois($nom_du_site, $prefixeTab);
297   
298            $html .= "<img border=\"0\" src=\"images/tmp/".$pngJour."\" alt=\"Graphique journalier\">\n";
299            $html .= "<br>\n";
300            $html .= "<img border=\"0\" src=\"images/tmp/".$pngMois."\" alt=\"Graphique mensuel\">\n";
301            $html .= "</body></html>\n";
302   
303            echo $html;
304
305            // a voir (pb synchro avec affichage de la page)
306            //exec("rm -f ".$png_path.$pngJour);
307            //exec("rm -f ".$png_path.$pngMois);
308        } else {
309            // si le fichier rrd n'est pas présent
310            $html .= "<br>  Données indisponibles pour le moment\n";
311            $html .= "</body></html>\n";
312           
313            echo $html;
314        }
315    }
316
317// FX
318//$nom_du_site = "U-RENNES-FOUGERES";
319//$p1 = array("129.20.0.0",16);
320//$p2 = array("129.20.207.0",24);
321//$p3 = array("148.60.0.0",16);
322//$p4 = array("148.60.206.0",24);
323//$p5 = array("148.60.207.0",24);
324//$prefixeTab = array($p1,$p2,$p3,$p4,$p5);
325//generation_graph_jour($nom_du_site, $prefixeTab);
326//generation_graph_mois($nom_du_site, $prefixeTab);
327// FX
328
329?>
Note: See TracBrowser for help on using the browser.