-
Notifications
You must be signed in to change notification settings - Fork 7
/
generate_monitor.php
311 lines (249 loc) · 9.62 KB
/
generate_monitor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
if ( ! is_readable('./config.php') ) {
echo("<H2>Error: Configuration file config.php does not exist. Please
notify your system administrator.</H2>");
exit;
} else
include_once('./config.php');
/* ---------------------------------------------------------------------- */
include_once('./tools.php');
##########################################################3
# Make sure cdiagram library is included
##########################################################3
require_once("./cdiagram/class/diagram.php");
require_once("DB.php");
##########################################################
# Create the new graph with the specified size. If smallsize is set use it
# to create small graphs that show up on the main utilization page
##########################################################
if ( isset($_GET['smallsize']) ) {
$size = explode(",", $smallgraph);
$image = new CDiagram($size[0],$size[1],"PNG");
} else {
$size = explode(",", $largegraph);
$image = new CDiagram($size[0],$size[1],"PNG");
}
$image->setDiagramType("Line"); # Balken");
$image->setBackground(255,255,255);
$image->setTextcolor(0,0,0);
$image->setLinecolor(0,0,0);
$image->setGraphcolor(0,255,0);
$image->setValuecolor(255,0,0);
$image->paintHeader(htmlspecialchars($_GET['feature']),4);
################################################################
# Connect to the database
# Use persistent connections
################################################################
$db = DB::connect($dsn, true);
if (DB::isError($db)) {
die ($db->getMessage());
}
################################################################
# When drawing utilization it is useful to know what was the usage in comparison
# to available licenses. Graphing package will try to auto-scale the Y-axis depending
# on the largest available Y value. Unfortunately that is not too useful since we will
# likely show higher usage that the number of available licenses. Thus you need
# to tell the graph script what its limits are
$sql = "SELECT flmavailable_num_licenses FROM licenses_available WHERE flmavailable_product='" .
htmlspecialchars($_GET['feature']) . "' AND flmavailable_date = '" .
htmlspecialchars($_GET['mydate']) . "'";
$upper_limit = $db->getOne($sql);
$lic_avail = isset($upper_limit);
$i = 0;
while ((!$lic_avail || $upper_limit <= 0 ) && ($i < 30)){
$i++;
if (!$lic_avail){
$current_date = mktime(0,0,0,date("m"),date("d") - $i, date("Y"));
$sql = "SELECT flmavailable_num_licenses FROM licenses_available WHERE flmavailable_product='" .
htmlspecialchars($_GET['feature']) . "' AND flmavailable_date = '" .
date("Y-m-d", $current_date) . "'";
$upper_limit = $db->getOne($sql);
$lic_avail = isset($upper_limit);
}
}
if ($i == 30) {
generate_error_image(" license_cache.php isn't run on : " . date("Y-m-d", $current_date) . " " . $upper_limit . " " . $lic_avail );
exit;
}
#if ( !isset($upper_limit) || $upper_limit <= 0 ) {
# generate_error_image("Total num licenses not available. license_cache.php isn't run");
# exit;
#}
$period = htmlspecialchars($_GET['period']);
$i = 1;
# Set some kind of value even if the set is empty
$image->setData(1,0);
$image->setItemData(0,0);
$current_date = mktime (0,0,0,date("m"),date("d"), date("Y"));
switch ($period) {
case 'day' :
$xlimit = 0;
break;
case 'week' :
$xlimit = 6;
break;
case 'month' :
$xlimit = date("t", $current_date) - 1;
break;
case 'year' :
$xlimit = 364;
break;
}
$daypoint = 1440 / $collection_interval;
$data = array(0);
$j = $xlimit;
$y_max = 0;
while ($j >= 0){
################################################################
# First let's get license usage for the product specified in $feature
##############################################################
$current_date = mktime (0,0,0,date("m"),date("d") - $j, date("Y"));
$today = mktime (0,0,0,date("m"),date("d"), date("Y"));
$sql = "SELECT flmusage_server,flmusage_time,SUM(flmusage_users) FROM license_usage WHERE flmusage_product='" .
htmlspecialchars($_GET['feature']) . "' AND flmusage_date = '" . date("Y-m-d", $current_date)
. "' GROUP BY flmusage_time";
$recordset = $db->query($sql);
if (DB::isError($recordset)) {
die ($recordset->getMessage());
}
$i_last = $i;
while ($row = $recordset->fetchRow()){
$data[$i] = $row[2];
if ($row[2] > $y_max)
$y_max = $row[2];
$i++;
}
$i_delta = $i - $i_last;
if (($i_delta < $daypoint) and ($current_date != $today) and ($y_max > 0)){
$i_limit = $daypoint - $i_delta;
for ($k = 0; $k < $i_limit; $k++){
$data[$i] = 0;
$i++;
}
}
$j--;
$recordset->free();
}
#print_r($data);
# Close the connection
# If there is data in the set draw the graph
if ( $i > 1 ) {
#$daypoint = 1440 / $collection_interval;
$scalingfactor = $daypoint / $xlegendpoints;
switch ($period) {
case 'day' :
$image->setX($i + 0.00001 ,0,1);
for ( $j = 1 ; $j <= $xlegendpoints * 2 ; $j++ ) {
$image->setItemData(1+$j*$scalingfactor/2, $j); # *2);
}
break;
case 'week' :
# Use first letter format for X labels
$image->setX($i + 0.00001 , 0, $daypoint);
$xlegendpoint = ceil($i / $daypoint);
for ( $j = 1 ; $j <= $xlegendpoint ; $j++ ) {
$axis_time = mktime (0,0,0,date("m"),date("d") - ($xlegendpoint - $j), date("Y"));
$image->setItemData($j * $daypoint, date("D", $axis_time));
}
break;
case 'month' :
# Use dd-mm format for X label (print only monday)
$image->setX($i + 0.00001 , 0, $daypoint);
$xlegendpoint = ceil($i / $daypoint);
for ( $j = 1 ; $j <= $xlegendpoint ; $j++ ) {
$axis_time = mktime (0,0,0,date("m"),date("d") - ($xlegendpoint - $j), date("Y"));
$axis_date = date("d-m", $axis_time);
if (date("D", $axis_time) == 'Mon')
$image->setItemData($j * $daypoint, $axis_date);
}
break;
case 'year' :
# Only print the end of the month
$image->setX($i + 0.00001 , 0, $daypoint); # 30*96
$xlegendpoint = ceil($i / $daypoint);
$nb_day = date("d", $current_date);
$x = ($xlegendpoint - $nb_day) * $daypoint;
$x_label = date("M", mktime (0,0,0,date("m") - 1, date("d"), date("Y")));
$image->setItemData($x, $x_label);
for ( $j = 1 ; $j <= 11 ; $j++ ) {
$nb_day = date("t", mktime (0,0,0,date("m") - $j, date("d"), date("Y")));
$x -= $nb_day * $daypoint;
$x_label = date("M", mktime (0,0,0,date("m") - ($j + 1), date("d"), date("Y")));
$image->setItemData($x, $x_label);
}
break;
}
$image->enable("ItemData");
# Upper and lower indices and scale step
$image->setY($upper_limit + 0.0001,0, ceil ($upper_limit/5));
#############################################################
# How many data values do we have:
# 24 hrs = 1440 min / collection interval ie.
# 1440 / 15 mins = 96 values
# I want to show legend only every 2 hours ie. 12 points
#############################################################
$image->paintXY();
# Axis legend
$image->paintScale("Time", "Number of licenses", "FF_FONT1");
# Graph color
$image->setGraphcolor(100,100,200);
$image->paintData();
################################################################################
# We will now draw a line that indicates the number of available licenses ie.
# at the top of the graph we will draw a solid line
################################################################################
# Clear the data from the bar graph
$image->clearData();
$image->setDiagramType("Line");
$image->setData(0,$upper_limit);
$image->setData($i, $upper_limit);
$image->setGraphcolor(255,0,0);
$image->paintData();
################################################################################
# We will now draw lines that indicates the scale.
# For all value on Y scale we will draw a solid line
################################################################################
if ($upper_limit >= 10)
{
for ($j = 1; $j < 10; $j++){
$image->clearData();
$image->setDiagramType("Line");
$image->setData(0, $upper_limit * $j / 10);
$image->setData($i, $upper_limit * $j / 10);
$image->setGraphcolor(222,222,222);
$image->paintData();
}
}else{
for ($j = 1; $j < $upper_limit; $j++){
$image->clearData();
$image->setDiagramType("Line");
$image->setData(0, $j);
$image->setData($i, $j);
$image->setGraphcolor(222,222,222);
$image->paintData();
}
}
$image->clearData();
$image->setDiagramType("Line"); # Balken
for ($j = 0; $j < $i; $j++){
$image->setData($j, $data[$j]);
}
$image->setGraphcolor(100,100,200);
$image->paintData();
# Should we display debug options
if ( isset($_GET['debug']) ) {
echo("If following array is empty your license utilization table is not
being populated. Most common problem is that license_util.php script is not
being run periodically ie. every 15 minutes.<p><PRE>"); print_r($image->m_data);
exit;
} else {
# If not draw image
Header("Content-type: image/png");
$image->show();
}
# If there is no data an image should be created indicating the problem.
} else {
generate_error_image("No data available to display for " . $_GET['feature'] . ". license_util.php is not being run");
}
$db->disconnect();
?>