e675020ef7857af2255fafad649fd8a9b46e0477
[auf-serveur.git] / glpi-plugin-auf / plugin / report.computers.php
1 <?php
2 //Options for GLPI 0.71 and newer : need slave db to access the report
3 $USEDBREPLICATE=1;
4 $DBCONNECTION_REQUIRED=0;
5
6 include ("../../inc/includes.php");
7
8 Session::checkRight("reports", "r");
9 Session::checkRight("computer", "r");
10
11 $report_title = "Nombre de machines par catégorie";
12
13 #Html::header(Report::getTypeName(2), $_SERVER['PHP_SELF'], "utils", "report");
14 Html::header($report_title, $_SERVER['PHP_SELF'], "utils", "report");
15 Report::title();
16
17 echo "\n<p class='big b' style='margin: 5px; text-align: center'><span id='report_date' style='float: right; margin: 2px 20px; font-size: 75%; font-style: italic'>(".date('Y-m-d H:i:s')." UTC)</span>$report_title</p>";
18
19 $computer = new Computer();
20
21 $itemtype = 'Computer';
22 $table_item = getTableForItemType($itemtype);
23
24 $svg_colors = Array('red'=>'#ffa0a0', 'green'=>'#a0ffa0', 'yellow'=>'#ffdd80', 'blue'=>'#a0a0ff');
25
26 function svg_circle($color, $size=4) {
27 global $svg_colors;
28 if (array_key_exists($color, $svg_colors)) { $color = $svg_colors[$color]; }
29 $r = (int)$size; $cx = $r+1; $cy = $r+1;
30 $svg = '<svg width="'.(2+2*$r).'" height="'.(2+2*$r).'" style="margin: 1px; vertical-align: middle;">';
31 $svg .= "<circle cx='$cx' cy='$cy' r='$r' stroke='black' stroke-width='0' fill='$color' />";
32 $svg .= '</svg>';
33 return $svg;
34 }
35
36 function svg_pie_chart($values, $size=16) {
37 global $svg_colors;
38 $color_names = Array('red', 'green', 'yellow', 'blue'); $color_idx = 0;
39 $total = 0.0; foreach ($values as $v) { $total += $v; }
40 $r = (int)$size; $cx = $r+1; $cy = $r+1; $rad = 2*pi()/$total; $old_a = 0;
41 $svg = '<svg width="'.(2+2*$r).'" height="'.(2+2*$r).'" style="margin: 2px; vertical-align: middle;">';
42 foreach ($values as $a) {
43 $x1 = $cx + $r * cos(-$old_a * $rad);
44 $y1 = $cy + $r * sin(-$old_a * $rad);
45 $x2 = $cx + $r * cos(-($old_a + $a) * $rad);
46 $y2 = $cy + $r * sin(-($old_a + $a) * $rad);
47 $big = ($a > ($total/2)) ? '1' : '0';
48 $color = $svg_colors[$color_names[$color_idx++]];
49 $svg .= "<path d=\"M$cx,$cy L$x1,$y1 A$r,$r 0 $big,0 $x2,$y2 z\" style=\"stroke: 0 black; fill: $color;\" />";
50 $old_a += $a;
51 }
52 $svg .= '</svg>';
53 return $svg;
54 }
55
56 // echo "\n<span class='big b' style='color: red;'>ATTENTION : ce rapport est en cours de refonte, merci de ne pas tenir compte des données affichées ci-dessous.</span><br><br>";
57
58 //echo "\n<span class='big b'>Ordinateurs par implantation</span><br><br>";
59 echo "<table class='tab_cadrehov'>";
60 echo "<tr class='tab_bg_1'><th rowspan='2' colspan='2'>Implantation</th>";
61 echo "<th colspan='2'>".svg_circle('red')." Serveurs</th>";
62 echo "<th colspan='2'>".svg_circle('green')." Postes internes</th>";
63 echo "<th colspan='2'>".svg_circle('yellow')." Postes publics</th>";
64 echo "<th rowspan='2'>Total</th>";
65 echo "<th rowspan='2'>".svg_circle('blue')." Virtuels</th>";
66 echo "<th rowspan='2'>Autres<br /><span style='font-size: 75%'>(sans statut)</span></th></tr>";
67 echo "<tr class='tab_bg_1'><th>Total</th><th>Production</th>";
68 echo "<th>Total</th><th>Attribué</th><th>Total</th><th>Attribué</th></tr>\n";
69
70 $grand_serveurs_total = 0; $grand_serveurs_prod = 0;
71 $grand_internes_total = 0; $grand_internes_prod = 0;
72 $grand_publics_total = 0; $grand_publics_prod = 0;
73 $grand_total = 0;
74 $grand_virtuels_total = 0; $grand_autres_total = 0;
75 # récupération de la liste des régions
76 $query = "SELECT id FROM glpi_entities WHERE (id=0 OR entities_id=0)";
77 $query .= " ".getEntitiesRestrictRequest("AND", "glpi_entities")." ORDER BY name";
78 $region_result = $DB->query($query);
79 # parcours des régions
80 while ($region_data = $DB->fetch_assoc($region_result)) {
81 $region_id = $region_data['id'];
82 if ($region_id != 0) {
83 $region_name = getTreeLeafValueName("glpi_entities", $region_id);
84 } else {
85 $region_name = "AUF";
86 }
87 $table_lines = Array();
88 $region_serveurs_total = 0; $region_serveurs_prod = 0;
89 $region_internes_total = 0; $region_internes_prod = 0;
90 $region_publics_total = 0; $region_publics_prod = 0;
91 $region_total = 0; $region_virtuels_total = 0;
92 $region_autres_total = 0;
93
94 # récupération de la liste des implantations
95 $query = "SELECT id,level FROM glpi_entities WHERE ";
96 if ($region_id != 0) {
97 $query .= getRealQueryForTreeItem("glpi_entities", $region_id, "id");
98 } else {
99 $query .= "id=0";
100 }
101 $query .= " ".getEntitiesRestrictRequest("AND", "glpi_entities")." ORDER BY name";
102 $implant_result = $DB->query($query);
103 # parcours des implantations
104 while ($implant_data = $DB->fetch_assoc($implant_result)) {
105 $implant_id = $implant_data['id'];
106 if ($implant_id != 0) {
107 $implant_name = getTreeLeafValueName("glpi_entities", $implant_id);
108 } else {
109 $implant_name = "<i>(non classé)</i>";
110 }
111
112 # récupération de la liste des systèmes de l'implantation
113 $nb_serveurs_total = 0; $nb_serveurs_prod = 0;
114 $nb_internes_total = 0; $nb_internes_prod = 0;
115 $nb_publics_total = 0; $nb_publics_prod = 0;
116 $nb_virtuels_total = 0; $nb_autres_total = 0;
117 $query = "SELECT s.completename AS sname, ac.est_virtuel AS virtuel,
118 acc.name AS categorie, acu.name AS utilisation
119 FROM `$table_item` c
120 LEFT JOIN glpi_states s ON (c.states_id = s.id)
121 LEFT JOIN glpi_plugin_auf_computers ac ON (c.id = ac.id)
122 LEFT JOIN glpi_plugin_auf_categories acc ON (ac.auf_categories_id = acc.id)
123 LEFT JOIN glpi_plugin_auf_utilisations acu ON (ac.auf_utilisations_id = acu.id)
124 WHERE c.is_deleted = '0' AND c.is_template = '0'
125 AND c.entities_id = '".$implant_id."'";
126 $result = $DB->query($query);
127 while ($data=$DB->fetch_assoc($result)) {
128 // on ne compte pas les sorties d'inventaire
129 if (preg_match("/^sortie/i", $data['sname'])) {
130 continue;
131 }
132 // serveurs
133 if (preg_match("/^infra/i", $data['categorie'])) {
134 $nb_serveurs_total++;
135 if (preg_match("/^production/i", $data['utilisation']) &&
136 !preg_match("/panne/i", $data['sname']) ) {
137 $nb_serveurs_prod++;
138 }
139 }
140 // postes internes
141 elseif (preg_match("/^poste.*interne/i", $data['categorie'])) {
142 $nb_internes_total++;
143 if (preg_match("/^production/i", $data['utilisation']) &&
144 !preg_match("/panne/i", $data['sname']) ) {
145 $nb_internes_prod++;
146 }
147 }
148 // postes publics
149 elseif (preg_match("/^poste.*public/i", $data['categorie'])) {
150 $nb_publics_total++;
151 if (preg_match("/^production/i", $data['utilisation']) &&
152 !preg_match("/panne/i", $data['sname']) ) {
153 $nb_publics_prod++;
154 }
155 }
156 // autres
157 else {
158 // machines virtuelles
159 if ($data['virtuel']) {
160 $nb_virtuels_total++;
161 // autres
162 } else {
163 $nb_autres_total++;
164 }
165 }
166 } // systèmes dans l'implantation
167
168 $total = $nb_serveurs_total + $nb_internes_total + $nb_publics_total;
169 if ($total > 0) {
170 $spaces = "";
171 for ($i=1;$i<$implant_data['level'];$i++) { $spaces .= "&nbsp;&nbsp;"; }
172 $line = "<tr><td class='tab_bg_1'>$spaces$implant_name</td>";
173 $line .= "<td class='numeric'>$nb_serveurs_total</td>";
174 $line .= "<td class='numeric'>$nb_serveurs_prod</td>";
175 $line .= "<td class='numeric'>$nb_internes_total</td>";
176 $line .= "<td class='numeric'>$nb_internes_prod</td>";
177 $line .= "<td class='numeric'>$nb_publics_total</td>";
178 $line .= "<td class='numeric'>$nb_publics_prod</td>";
179 $line .= "<td class='numeric tab_bg_1'>$total</td>";
180 $line .= "<td class='numeric'>$nb_virtuels_total</td>";
181 $line .= "<td class='numeric'>$nb_autres_total</td></tr>";
182 $table_lines[] = $line;
183 }
184 $region_serveurs_total += $nb_serveurs_total;
185 $region_serveurs_prod += $nb_serveurs_prod;
186 $region_internes_total += $nb_internes_total;
187 $region_internes_prod += $nb_internes_prod;
188 $region_publics_total += $nb_publics_total;
189 $region_publics_prod += $nb_publics_prod;
190 $region_total += $total;
191 $region_virtuels_total += $nb_virtuels_total;
192 $region_autres_total += $nb_autres_total;
193 //$table_lines[] = "<tr><td>".$implant_name."</td></tr>";
194 } // fin des implantations de la région
195
196 $line = "<tr class='tab_bg_1 b'><td>Sous-total</td>";
197 $line .= "<td class='numeric'>$region_serveurs_total</td>";
198 $line .= "<td class='numeric'>$region_serveurs_prod</td>";
199 $line .= "<td class='numeric'>$region_internes_total</td>";
200 $line .= "<td class='numeric'>$region_internes_prod</td>";
201 $line .= "<td class='numeric'>$region_publics_total</td>";
202 $line .= "<td class='numeric'>$region_publics_prod</td>";
203 $line .= "<td class='numeric tab_bg_1'>$region_total</td>";
204 $line .= "<td class='numeric'>$region_virtuels_total</td>";
205 $line .= "<td class='numeric'>$region_autres_total</td></tr>";
206 $table_lines[] = $line;
207
208 $region_name = svg_pie_chart(Array($region_serveurs_total, $region_internes_total, $region_publics_total, $region_virtuels_total), 16) . $region_name;
209 $table_lines[0] = str_replace("<tr>", "<tr><td class='tab_bg_1 b' rowspan='".count($table_lines)."'>$region_name</td>", $table_lines[0]);
210 if ($region_id != 0) {
211 echo "<tr><th colspan='11'></th></tr>\n";
212 }
213 echo implode("\n", $table_lines);
214
215 $grand_serveurs_total += $region_serveurs_total;
216 $grand_serveurs_prod += $region_serveurs_prod;
217 $grand_internes_total += $region_internes_total;
218 $grand_internes_prod += $region_internes_prod;
219 $grand_publics_total += $region_publics_total;
220 $grand_publics_prod += $region_publics_prod;
221 $grand_total += $region_total;
222 $grand_virtuels_total += $region_virtuels_total;
223 $grand_autres_total += $region_autres_total;
224 } // fin des régions
225
226 // echo "<tr class='tab_bg_1'><td colspan='8'>&nbsp;</td></tr>";
227
228 /*
229 # total operating systems
230 $query = "SELECT COUNT(DISTINCT fia.`tag`) FROM `".$table_item."`
231 LEFT JOIN `glpi_computers_items` ci
232 ON (ci.`itemtype` = '".$itemtype."'
233 AND ci.`items_id` = `".$table_item."`.`id`)
234 LEFT JOIN `glpi_plugin_fusioninventory_agents` fia
235 ON (fia.`computers_id` = `".$table_item."`.`id`)
236 WHERE `".$table_item."`.`is_deleted` = '0'
237 AND `".$table_item."`.`is_template` = '0' ".
238 getEntitiesRestrictRequest("AND", $table_item);
239 $result = $DB->query($query);
240 $grand_total = $DB->result($result, 0, 0);
241 */
242 $line = "<tr class='tab_bg_1'><td class='b' colspan='2'>Total général</td>";
243 $line .= "<td class='numeric b'>$grand_serveurs_total</td>";
244 $line .= "<td class='numeric b'>$grand_serveurs_prod</td>";
245 $line .= "<td class='numeric b'>$grand_internes_total</td>";
246 $line .= "<td class='numeric b'>$grand_internes_prod</td>";
247 $line .= "<td class='numeric b'>$grand_publics_total</td>";
248 $line .= "<td class='numeric b'>$grand_publics_prod</td>";
249 $line .= "<td class='numeric b'>$grand_total</td>";
250 $line .= "<td class='numeric b'>$grand_virtuels_total</td>";
251 $line .= "<td class='numeric b'>$grand_autres_total</td></tr>";
252 echo "<tr><th colspan='11'></th></tr>\n";
253 echo $line;
254
255 echo "</table>";
256
257 Html::footer();
258 ?>