root/tags/renetcol-0.0.11/trunk/tool/metro.php

Revision 31, 5.0 KB (checked in by andreu, 16 years ago)

now, only subnets rrd files from routers who are seeing will be created - new scripts in tool

  • Property svn:eol-style set to native
Line 
1<?php
2    function generation_graph_jour($nom_du_site, $prefixe, $cidr, $rrdname, $sampling) {
3        // SAGA
4        $png_path = "/var/www/saga/www/images/tmp/";
5        // SAGA
6
7        // FX
8        //$png_path = "/tmp/";
9        // FX
10
11        # creation du nom de fichier de l'image a creer
12        $filename = $nom_du_site."_j".".png";
13        $pngname = $png_path.$filename;
14
15        # creation de l'image dans $png_path.
16        $res = `rrdtool graph $pngname -s -1D --title "$nom_du_site - prefixe : $prefixe / $cidr" --vertical-label "bits/s" DEF:inBytesG=$rrdname:inBytes:AVERAGE DEF:outBytesG=$rrdname:outBytes:AVERAGE CDEF:input10=inBytesG,8,*,5,/,60,/,$sampling,* CDEF:output10=outBytesG,8,*,5,/,60,/,$sampling,* AREA:input10\#00FF00:"In b/s\\n" LINE1:output10\#0000FF:"Out b/s\\n"`;
17       
18        # todo FX : faire un retour different si erreur
19        return $filename;
20    }
21
22    function generation_graph_mois($nom_du_site, $prefixe, $cidr, $rrdname, $sampling) {
23        // SAGA
24        global $cfgTmp, $cfgFileSep;
25
26        $png_path = "/var/www/saga/www/images/tmp/";
27        $tmp_path .= $cfgTmp.$cfgFileSep;
28        // SAGA
29
30        // FX
31        //$png_path = "/tmp/";
32        //$tmp_path = "/tmp/";
33        // FX
34       
35        # creation du nom de fichier de l'image a creer
36        $filename = $nom_du_site."_m".".png";
37        $pngname = $png_path.$filename;
38       
39        # recuperation des donnees par un rrd fecht pour calculer le debit 95%percentile
40        $tmp_file = $tmp_path.$nom_du_site.".tmp";
41
42        `rrdtool fetch $rrdname AVERAGE --start -31Days | cut -d " " -f 2,3 > $tmp_file`;
43
44        $fd = fopen ($tmp_file, "r");
45        $i=0;
46        $tab_in = array();
47        $tab_out = array();
48        while (!feof ($fd)) {
49            $buffer = fgets($fd, 4096);
50            $buffer_split = split(" ",$buffer);
51            $tab_in[$i] = trim($buffer_split[0]);
52            $tab_out[$i] = trim($buffer_split[1]);
53            $i++;
54        }
55        fclose ($fd);
56        rsort($tab_in,SORT_NUMERIC);
57        rsort($tab_out,SORT_NUMERIC);
58        foreach ( $tab_in as $key => $value ) {
59          if ($value != "nan") {$lgin++;}
60        }
61        $percin = $lgin-intval(($lgin*95/100));
62        foreach ( $tab_out as $key2 => $value2 ) {
63          if ($value2 != "nan") {$lgout++;}
64        }
65        $percout = $lgout-intval(($lgout*95/100));
66        $dr1 max($tab_in[$percin-1],$tab_out[$percout-1]);
67        $debit = number_format($dr1*$sampling);
68        `rm $tmp_file`;
69
70        # creation de l'image dans $png_path.
71        $res = `rrdtool graph $pngname -s -31D --title "$nom_du_site - prefixe : $prefixe / $cidr" --vertical-label "bits/s" DEF:inBytesG=$rrdname:inBytes:AVERAGE DEF:outBytesG=$rrdname:outBytes:AVERAGE CDEF:input10=inBytesG,8,*,5,/,60,/,$sampling,* CDEF:output10=outBytesG,8,*,5,/,60,/,$sampling,* AREA:input10\#00FF00:"In b/s\\n" LINE1:output10\#0000FF:"Out b/s\\n"  HRULE:$dr1\#FF0000:"RENATER debit (95th percentile) $debit bits/s"`;
72       
73        # todo FX : faire un retour different si erreur
74        return $filename;
75    }
76
77    function graphiqueMetrologie() {
78        // SAGA
79        global $cfgTmp, $cfgFileSep, $cfgRrdPath;
80
81        $png_path = "/var/www/saga/www/images/tmp/";
82        $nfs_path = $cfgRrdPath.$cfgFileSep;
83
84        $monAg = new Agrement_2($this->tuple['ip_ag_numero'],$this->tuple['ip_ag_version'], $this->dbh);
85        $monAg->querySelect('r', true);
86        $nom_du_site = $monAg->get_dc_et_code();
87        $prefixe = $this->tuple['ip_adresse'];
88        $cidr = $this->tuple['ip_cidr'];
89        // SAGA
90
91        // FX
92        //$png_path = "/tmp/";
93        //$nfs_path = ""; # expl: "/mnt/db/RENATER_PREFIXES_RRD/";
94        //$nom_du_site = "U-BS-VANNES";
95        //$prefixe = "193.54.227.0";
96        //$cidr = "24";
97        // FX
98       
99        # creation du nom de fichier .rrd correspondant (du type ".../193_49_159_0_24_X.rrd")
100        # X n'est pas connu, c'est la valeur d'echantillonnage
101        # ce fichier sera sur le serveur de donnees d ou $nfs_path
102        $tmp = str_replace(".", "_", $prefixe);
103        $prefix_name = $tmp . "_" . $cidr;
104        if (is_dir($nfs_path)) {
105            if ($dh = opendir($nfs_path)) {
106                while (($file = readdir($dh)) !== false) {
107                    if (strpos($file, $prefix_name) !== false) {
108                        $found = 1;
109                        break;
110                    };
111                }
112                closedir($dh);
113            }
114        }
115        // SAGA
116        $title = "Graphiques Métrologie";
117        require_once("saga_2.inc.php");
118        include("head_2.inc.php");
119        // SAGA
120
121        // FX
122        //$html = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">";
123        //$html = "<html>";
124        //$html = "<head>";
125        //$html = "<title>Graphiques Métrologie</title>";
126        //$html = "<body>\n";
127        // FX
128
129        if ($found == true) {
130            $rrdname = $nfs_path.$file;
131            $file_split = split("_", $file);
132            $tmp = split("\.",$file_split[5]);
133            $sampling = $tmp[0];
134
135            $pngJour = $this->generation_graph_jour($nom_du_site, $prefixe, $cidr, $rrdname, $sampling);
136            $pngMois = $this->generation_graph_mois($nom_du_site, $prefixe, $cidr, $rrdname, $sampling);
137   
138            $html .= "<img border=\"0\" src=\"images/tmp/".$pngJour."\" alt=\"Graphique journalier\">\n";
139            $html .= "<br>\n";
140            $html .= "<img border=\"0\" src=\"images/tmp/".$pngMois."\" alt=\"Graphique mensuel\">\n";
141            $html .= "</body></html>\n";
142   
143            echo $html;
144
145            // a voir (pb synchro avec affichage de la page)
146            //exec("rm -f ".$png_path.$pngJour);
147            //exec("rm -f ".$png_path.$pngMois);
148        } else {
149            // si le fichier rrd n'est pas présent
150            $html .= "<br>  Données indisponibles pour le moment\n";
151            $html .= "</body></html>\n";
152           
153            echo $html;
154        }
155    }
156?>
Note: See TracBrowser for help on using the browser.