forked from proche-rainmaker/phplicensewatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.php
1246 lines (1094 loc) · 40.3 KB
/
tools.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
#
# $Id: tools.php 61155 2013-03-19 22:36:07Z proche $
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
# B00zy's timespan script v1.2 #
# #
# timespan -- get the exact time span between any two moments in time. #
# #
# Description: #
# #
# class timespan, function calc ( int timestamp1, int timestamp2) #
# #
# The purpose of this script is to be able to return the time span #
# between any two specific moments in time AFTER the Unix Epoch #
# (January 1 1970) in a human-readable format. You could, for example, #
# determine your age, how long you have been married, or the last time #
# you... you know. ;) #
# #
# The class, "timespan", will produce variables within the class #
# respectively titled years, months, weeks, days, hours, minutes, #
# seconds. #
# #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
# Example 1. B00zy's age. #
# #
# $t = new timespan( time(), mktime(0,13,0,8,28,1982)); #
# print "B00zy is $t->years years, $t->months months, ". #
# "$t->days days, $t->hours hours, $t->minutes minutes, ". #
# "and $t->seconds seconds old.\n"; #
# #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
define('day', 60*60*24 );
define('hour', 60*60 );
define('minute', 60 );
class timespan
{
var $years;
var $months;
var $weeks;
var $days;
var $hours;
var $minutes;
var $seconds;
function leap($time)
{
if (date('L',$time) and (date('z',$time) > 58))
return (double)(60*60*24*366);
else
{
$de = getdate($time);
$mkt = mktime(0,0,0,$de['mon'],$de['mday'],($de['year'] - 1));
if ((date('z',$time) <= 58) and date('L',$mkt))
return (double)(60*60*24*366);
else
return (double)(60*60*24*365);
}
}
function readable()
{
$values = array('years','months','weeks','days','hours','minutes','seconds');
foreach ($values as $k => $v)
if ($this->{$v}) $fmt .= ( $fmt? ', ': '') . $this->{$v} . " $v";
return $fmt . ( $fmt? '.': '') ;
}
function timespan($after,$before)
{
# Set variables to zero, instead of null.
$this->years = 0;
$this->months = 0;
$this->weeks = 0;
$this->days = 0;
$this->hours = 0;
$this->minutes = 0;
$this->seconds = 0;
$duration = $after - $before;
# 1. Number of years
$dec = $after;
$year = $this->leap($dec);
while (floor($duration / $year) >= 1)
{
# We don't need this VV
#print date("F j, Y\n",$dec);
$this->years += 1;
$duration -= (int)$year;
$dec -= (int)$year;
$year = $this->leap($dec);
}
# 2. Number of months
$dec = $after;
$m = date('n',$after);
$d = date('j',$after);
while (($duration - day) >= 0)
{
$duration -= day;
$dec -= day;
$this->days += 1;
if ( (date('n',$dec) != $m) and (date('j',$dec) <= $d) )
{
$m = date('n',$dec);
$d = date('j',$dec);
$this->months += 1;
$this->days = 0;
}
}
# 3. Number of weeks.
$this->weeks = floor($this->days / 7);
$this->days %= 7;
# 4. Number of hours, minutes, and seconds.
$this->hours = floor($duration / (60*60));
$duration %= (60*60);
$this->minutes = floor($duration / 60);
$duration %= 60;
$this->seconds = $duration;
}
}
function generate_error_image($str) {
header("Content-type: image/png");
$im = @imagecreate (300, 200)
or die ("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate ($im, 220, 210, 60);
$text_color = imagecolorallocate ($im, 233, 14, 91);
imagestring ($im, 1, 5, 5, $str, $text_color);
imagestring ($im, 1, 5, 25, "Please check your settings.", $text_color);
imagepng($im);
imagedestroy($im);
}
###################################################################################################
# Insert values into existing RRDs. If no date is supplied it defaults to current time
###################################################################################################
function insert_into_rrd($name, $payload, $date = "N") {
global $rrdtool_bin;
global $rrd_dir;
$ret_var=0;
$filename = $rrd_dir . "/" . $name . ".rrd";
$res = create_rrd($filename);
if ($res) {
// create_rrd failed
// so fail insert_into_rrd
$ret_var=2;
} else {
exec ($rrdtool_bin . " update " . $filename . " " . $date . ":" . $payload, $output=array(),$ret_var);
}
switch ($ret_var) {
case 1:
print $name . ".rrd unable to be updated <br>\n";
break;
case 2:
print $name . ".rrd unable to be created <br>\n";
break;
}
return $ret_var;
}
function bulk_rrd($name,$payload) {
global $rrdtool_bin;
global $rrd_dir;
$ret_var=0;
$filename = $rrd_dir . "/" . $name . ".rrd";
$res = create_rrd($filename);
exec($rrdtool_bin . " update " . $filename . " " . $payload,$output=array(),$retvar);
return $ret_var;
}
function create_rrd($filename) {
global $collection_interval;
global $rrd_dir;
global $rrdtool_bin;
# set ret_var to 0
# meaning file exists
$ret_var=0;
if ( ! file_exists($filename) ) {
##################################################################
# Convert the collection interval into seconds
##################################################################
$step = $collection_interval * 60;
# Startime is today - a year
$startdate = time() - 365 * 1440 * 60;
$rrd_arg = $rrdtool_bin . " create " . $filename . " --start " . $startdate .
" --step " . $step .
" DS:used:GAUGE:900:0:10000" .
" DS:max:GAUGE:900:0:10000 " .
"RRA:AVERAGE:0.5:1:800 RRA:AVERAGE:0.5:6:800 RRA:AVERAGE:0.5:24:800 RRA:AVERAGE:0.5:288:800 " .
"RRA:MAX:0.5:1:800 RRA:MAX:0.5:6:800 RRA:MAX:0.5:24:800 RRA:MAX:0.5:288:800";
exec($rrd_arg,$output=array(),$ret_var);
}
return $ret_var;
}
function writeLicense_Usage($usage) {
# db connection
require_once("DB.php");
global $dsn;
global $db_hostname;
global $db_username;
global $db_password;
# rrd
global $rrdtool_bin;
$date = date('Y-m-d');
$time = date('H:i') . ":00";
$db = DB::connect($dsn, true);
if (DB::isError($db)) {
die ($db->getMessage());
}
$sql1 = "INSERT INTO license_usage (flmusage_server,flmusage_product,flmusage_date,flmusage_time,flmusage_users) ";
if (isset($usage) && is_array($usage)) {
# build sql stmt
$sql = $sql1 . "VALUES ('$usage[0]','$usage[1]','$date','$time',$usage[2])";
# build rrd stmt
# rrd filename is combination of server and feature
$cleanName = cleanHostname($usage[0]);
$name = $cleanName . "-" . strtolower($usage[1]);
$payload = $usage[2].":".$usage[3];
if (isset($_GET["debug"]) && ($_GET["debug"]==1)) {
print_sql($sql);
print $name . " " .$payload . "<br>\n";
} else {
if ( isset($db_hostname) && isset($db_username) && isset($db_password) ) {
$recordset = $db->query($sql);
if (DB::isError($recordset)) {
print_sql($sql);
die ($recordset->getMessage());
}
}
if ( isset($rrdtool_bin) && is_executable($rrdtool_bin)) {
$res = insert_into_rrd($name,$payload);
}
}
}
$db->disconnect();
unset($array);
}
function cleanHostname($name) {
if (strstr($name,'@')) {
# remove port and domain (1234@foo.example.com > foo)
preg_match("/\d+\@(\w+).*/",$name,$host);
} elseif (strstr($name,'.')) {
# remove domain (foo.example.com > foo)
preg_match("/(\w+).*/",$name,$host);
} elseif (strstr($name,' ')) {
# remove spaces (foo1 foo2 foo3 > foo1_foo2_foo3)
# pad array to 1
$host = array(1=>preg_replace("/ /","_",$name));
} else {
# pad array to 1
$host = array(1=>$name);
}
return strtolower($host[1]);
}
// function findServers
// called from index.php
// used to filter main server array down to more managable chunks
// can filter out all 'flexlm' licenses, or search for all services
// hosted from a single server
function findServers($needle,$key="type",$needle2=NULL,$key2=NULL) {
global $server;
$pos=array();
for ($i=0;$i<sizeof($server);$i++) {
if (!($needle2===NULL) && !($key2===NULL)) {
if ((stristr($server[$i][$key],$needle)) && (stristr($server[$i][$key2],$needle2))) {
//we need to get the real array position in there somehow
//this pos is used for the links
$splice=array_splice($server[$i],-1,-1,array($i));
$pos[]=$server[$i];
}
}
else {
if (stristr($server[$i][$key],$needle)) {
//we need to get the real array position in there somehow
//this pos is used for the links
$splice=array_splice($server[$i],-1,-1,array($i));
$pos[]=$server[$i];
}
}
}
return $pos;
}
function getDetails($server) {
# server[0] only defined if host was returned from findServers
# so check to see if it exists, others default to 0
if (isset($server[0])) {
$pos = $server[0];
} else {
$pos = 0;
}
switch($server['type']) {
case "flexlm":
$ret=get_flexlm($server['hostname'],$pos);
return $ret;
break;
case "spm":
$ret=get_spm($server['hostname'],$pos);
return $ret;
break;
case "rvl":
$ret=get_rvl($server['hostname'],$pos);
return $ret;
break;
case "sesi":
$ret=get_sesi($server['hostname'],$pos);
return $ret;
break;
case "tweak":
$ret=get_tweak($server['hostname'],$pos);
return $ret;
break;
case "rlm":
$ret=get_rlm($server['hostname'],$pos);
return $ret;
break;
case "pixar":
$ret=get_pixar($server['hostname'],$pos);
return $ret;
break;
default:
die("no server type found");
}
}
// function getTime
// called from common.php and version.php
// used to show how long page took to execute
function getTime() {
$a = explode (' ',microtime());
return(double) $a[0] + $a[1];
}
// fucntion AppendStatusMsg
// used on index.php and by monitoring
// joins individual status msg into one string
function AppendStatusMsg($statusMsg,$msg="") {
if ($msg!="") {
//string needs to be a single line for javascript to display
$statusMsg = $statusMsg . $msg . "<br>";
}
return $statusMsg;
}
function emailAlerts($host,$statusMsg) {
global $notify_to;
global $notify_from;
global $notify_alert;
global $URL;
if (($notify_to) && ($statusMsg) && (muffleAlerts($host))) {
$to = $notify_alert . "," . $notify_to;
$subject = "ALERT: License Server at " . $host['hostname'] . " is DOWN" ;
$headers = "From: License Robot <" . $notify_from . ">\r\n" .
"Reply-To: " . $notify_from . "\r\n" .
"X-Mailer: PHP/" . phpversion();
$msg = "Hostname: " . $host['hostname'] .
"\nDescription: " . $host['desc'] .
"\nType: " . $host['type'] .
"\nMessage: " . $statusMsg .
"\n\nURL: $URL";
mail($to,$subject,$msg,$headers);
}
}
//muffleAlerts
//return true if we want to muffle alerts for this host
function muffleAlerts($host) {
require_once("DB.php");
global $dsn;
global $notify_resend;
$mysqldate = date( 'Y-m-d H:i:s' );
$alertdate = date('Y-m-d H:i:s', strtotime("-$notify_resend minutes"));
$db = DB::connect($dsn, true);
if (DB::isError($db)) {
die ($db->getMessage());
}
# check for records
$sql ="SELECT pkid from alert_events where type = '" . $host['type'] . "' and hostname = '" . $host['hostname'] . "' and datetime > '" . $alertdate . "' and datetime < '" . $mysqldate . "'";
$recordset = $db->query($sql);
if (DB::isError($recordset)) {
print_sql($sql);
die ($recordset->getMessage());
}
if ($recordset->numRows() == 0 ) {
#nothing found
$sql ="INSERT into alert_events(datetime,type,hostname) values('" . $mysqldate . "','". $host['type'] . "','" . $host['hostname'] . "')";
$recordset = $db->query($sql);
if (DB::isError($recordset)) {
print_sql($sql);
die ($recordset->getMessage());
}
return 1;
} else {
# we found a match!
# muffle alerts
return 0;
}
}
function get_flexlm($server,$pos=0) {
global $lmutil;
global $today;
#preload arrays
$status_array=array();
$license_array=array();
$expiration_array=array();
$user_array=array();
# for [status]
$service = "";
$detaillink="<a href=\"details.php?listing=0&server=" . $pos . "\">Details</a>" ;
$listingexpirationlink="<a href=\"details.php?listing=1&server=" . $pos . "\">Expiration dates</a>" ;
$version="";
$master="";
$msg="";
$total_licenses = 0;
$fp = popen($lmutil . " lmstat -i -a -c " . $server,"r");
while (!feof($fp)) {
$line=fgets($fp,1024);
# is it up? build [status]
if (preg_match("/^(\w+|\w+\.\w+\.\w+): license server UP.*(\w\d+\.\d+).*/",trim($line),$res)) {
$service="up";
$master=$res[1];
if (strpos($res[1],'.')) {
$master = substr($res[1],0,strpos($res[1],'.'));
}
$version=$res[2];
}
if (preg_match("/Cannot connect to license server/",$line,$res)) {
$service = "down";
$detaillink="Details not available";
$listingexpirationlink="Expiration dates not available";
$msg="Cannot connect to $server";
break;
}
if (preg_match("/Cannot read data/", $line, $out) ) {
$service = "down";
$detaillink="Details not available";
$listingexpirationlink="Expiration dates not available";
$msg="Cannot read data from $server";
break;
}
if (preg_match("/Error getting status/", $line, $out) ) {
$service = "down";
$detaillink="Details not available";
$listingexpirationlink="Expiration dates not available";
$msg="Error getting status from $server";
break;
}
/* Checking if vendor daemon has died evenif lmgrd is still running */
if (preg_match("/vendor daemon is down/", $line, $out) ) {
$service = "warning";
$msg = "Vendor Daemon is down on $server";
break;
}
### END OF STATUS ###
### START OF LICENSES ###
# look for features in the output. you will see stuff like
# users of allegro_viewer: (total of 5 licenses available
if ( preg_match('/(users of) (.*)(\(total of) (\d+) (.*) (total of) (\d+) /i', $line, $out) && !preg_match('/no such feature exists/i', $line) ) {
$res = explode(":",trim($out[2]));
$feature = $res[0];
$license_array[$feature][] = array(
"num_licenses"=> $out[4],
"licenses_used" => $out[7]);
}
# nji: sometimes a vendor gives a "uncounted" file, with infinite licenses.
if ( preg_match('/(users of) (.*)(\(uncounted, node-locked)/i', $line, $out) ) {
$feature = $out[2];
$license_array[$feature][] = array(
"num_licenses" => "uncounted",
"licenses_used" => "uncounted");
}
if ( preg_match('/(\w+)\s+(\d+|\d+.\d+)\s+(\d+)\s+(\d+-\w+-\d+)\s+(\w+)$/i', $line,$out) ) {
# replace 1-jan-0 with 1-jan-2036
$out[4] = str_replace("-jan-0","-jan-2036",$out[4]);
# how many days remaining?
$days_to_expiration = ceil ((strtotime($out[4]) - $today) / 86400);
$expiration_array[$out[1]][] = array(
"vendor_daemon"=>$out[5],
"version"=>$out[2],
"expiration_date"=>$out[4],
"num_licenses"=>$out[3],
"days_to_expiration"=> $days_to_expiration,
"type"=>""
);
}
### END OF LICENSES ###
### START OF USERS ###
if ( preg_match('/.* (\w\d+.\d+), vendor.*/', $line,$out) ) {
$user_feature = $out[1];
}
# count the number of licenses. each used license has ", start" string in it
if ( preg_match('/.*, start (\w+\s+\d+\/\d+\s\d+:\d+)/', $line,$out ) ){
# Convert the date and time ie 12/5 9:57 to UNIX time stamp
$time_checkedout = strtotime ($out[1]);
$user_array[$feature][] = array(
"line" => trim($line) . " (" . $user_feature . ")",
"time_checkedout" => $time_checkedout);
}
### END OF USERS ###
}
/* If I don't get explicit reason that the server is UP set the status to DOWN */
if ( $service == "" ) {
$class = "down";
$detaillink="Details not available";
$listingexpirationlink="Expiration dates not available";
$msg="Unknown error from $server";
}
$status_array = array(
"service"=>$service,
"clients"=>$detaillink,
"listing"=>$listingexpirationlink,
"version"=>$version,
"master"=>$master,
"msg"=>$msg
);
# close open filehandle
pclose($fp);
$master = array(
"status"=>$status_array,
"licenses"=>$license_array,
"expiration"=>$expiration_array,
"users"=>$user_array
);
return $master;
}
function get_spm($server,$pos=0) {
global $spmstat;
global $today;
global $permdate;
# preload arrays
$status_array=array();
$license_array=array();
$expiration_array=array();
$user_array=array();
// dirty hack to force the order of return
$exec="";
if (preg_split("/ /",trim($server))) {
$spm_server = explode(" ",$server);
foreach ($spm_server as $spm) {
$exec .= sprintf("%s -s -Kd %s 2>&1 ; ",$spmstat,$spm);
}
} else {
$exec = sprintf("%s -s -Kd %s 2>&1",$spmstat,$server);
}
$fp = popen($exec, "r");
$service = "";
$clients="<a href=\"details.php?listing=0&server=" . $pos . "\">Details</a>" ;
$listing="<a href=\"details.php?listing=1&server=" . $pos . "\">Expiration dates</a>" ;
$masters=array();
$version="";
$msg="";
while ( !feof ($fp) ) {
$line = fgets ($fp, 1024);
### START STATS ###
/* Look for an expression like this ie. SPM-daemon on 'spmhost(spmhost.domain.com)' */
if (preg_match("/SPM-daemon on \'(\w+).*/", $line,$res ) ) {
$service = "up";
$master=$res[1];
array_push($masters,$master);
}
if (preg_match("/SPMD-version:(\d+\.\d+\.\d+).*/", $line,$res)) {
$version=$res[1];
}
if (preg_match("/Can\'t Connect to (\w+).*/i", $line, $res) ) {
$service = "down";
$clients="Details not available";
$listing="Expiration dates not available";
$msg="Cant connect to $res[1]";
break;
}
### END STATUS ###
### START EXPIRATION/LICENSE ###
if ( preg_match("/.* FL/",$line, $out) ) {
$license = preg_split ("/\s+/", trim($out[0]));
# PERM XSI license doesnt have date fields, check size of $license
if (sizeof($license) > 5) {
$days_to_expiration = ceil((strtotime($license[6]) - $today ) / 86400 );
} else {
$days_to_expiration = ceil((strtotime($permdate) - $today) / 86400 );
$license[6] = "permanent";
}
# build expiration_array
$expiration_array[$license[0]][] = array(
"vendor_daemon" => "spm",
"version" => $license[1],
"expiration_date" => $license[6],
"num_licenses" => $license[2],
"licenses_used" => $license[3],
"days_to_expiration" => $days_to_expiration,
"type" => $master );
# build license_array
$feature = $license[0];
$license_array[$feature][] = array(
"num_licenses" => $license[2],
"licenses_used" => $license[3],
"extra" => $master
);
}
### END EXPIRATION/LICENSE ###
### START CLIENTS ###
if ( preg_match("/.*connected since (\d+\/\d+\/\d+\s+\d+:\d+:\d+)/", $line,$out) ) {
$time_checkedout = trim($out[1]);
$user_array[$feature][] = array("line" => trim($line),"time_checkedout" => strtotime($time_checkedout));
}
### END CLIENTS ###
}
# close open filehandle
pclose($fp);
/* If I don't get explicit reason that the server is UP set the status to DOWN */
if ( $service == "" ) {
$service = "warning";
$clients="Details not available";
$listing="Expiration dates not available";
$msg="No data returned from $server";
}
$masters=array_unique($masters);
$ret="";
foreach ($masters as $key) {
$ret .= $key . " <br>";
}
$status_array = array(
"service"=>$service,
"clients"=>$clients,
"listing"=>$listing,
"version"=>$version,
"master"=>$ret,
"msg"=>$msg
);
$master = array("status"=>$status_array,"licenses"=>$license_array,"expiration"=>$expiration_array,"users"=>$user_array);
return $master;
}
function get_rvl($server, $pos=0) {
global $rvlstatus;
global $today;
global $permdate;
# preload arrays
$status_array=array();
$license_array=array();
$expiration_array=array();
$user_array=array();
$service="";
$clients="<a href=\"details.php?listing=0&server=".$pos."\">Details</a>";
$listing="<a href=\"details.php?listing=1&server=".$pos."\">Expiration dates</a>";
$master="";
$version="";
$msg="";
//add redirection so we can get stderr
$fp = popen("$rvlstatus 2>&1","r");
$x = -1;
while (!feof($fp)) {
$line = fgets ($fp,1024);
$line = trim($line);
### START STATUS ###
if (preg_match("/RE:Vision Effects Floating License Status (\d+\.\d+)/",$line,$res)) {
$d_version=$res[1];
}
if (preg_match("/License #1/",$line,$res)) {
$service="up";
}
if (preg_match("/Server: (\w+).*/",$line,$res)) {
$master=$res[1];
}
if (preg_match("/Could not find network license information/",$line,$res)) {
$service="down";
$clients="Details not available";
$listing="Expiration dates not available";
$msg="Could not find network license information from $server";
break;
}
if (preg_match("/No network license server is defined for this machine/",$line,$res)) {
$service="down";
$clients="Details not available";
$listing="Expiration dates not available";
$msg="No network license server is defined for $server";
break;
}
if (preg_match("/ERROR/",$line,$res)) {
$service="down";
$clients="Details not available";
$listing="Expiration dates not available";
$msg="Unable to connect to $server";
break;
}
### END STATUS ###
### START LICENSE/EXPIRATION ###
if (preg_match("/^Type: \w+:\s+(\d+)\s+.*/i",$line,$res)) {
$x++;
$num_licenses[$x] = $res[1];
}
if (preg_match("/^Product\(s\): (\w+)/",$line,$res)) {
$vendor[$x]=$res[1];
}
if (preg_match("/^Version: (\w\d.\d)/",$line,$res)) {
$l_version[$x] =$res[1];
}
if (preg_match("/^Host system: (\w+)/",$line,$res)) {
$type[$x]=$res[1];
}
if (preg_match("/^Expiry date: (\w+):\s+(\w+):\s+(\w+)/",$line,$res)) {
$date = $res[3];
if ($date == "never") {
$exp_date[$x] = $permdate;
} else {
$exp_date[$x] = $date;
}
}
if (preg_match("/^Product type: (\w+)/",$line,$res)) {
$feature[$x] = $res[1];
}
if (preg_match("/^Current clients: (\d+)/",$line,$res)) {
$licenses_used[$x] = $res[1];
}
### END EXPIRATION ###
### START USERS ###
# grab checked out lics now
if ( preg_match('/.*\:\s(\w+\s+\w+\s+\d+\s+\d+:\d+:\d+\s\d{4})/i',$line,$out)) {
$user_array[$feature[$x]][] = array(
"line" => trim($out[0]),
"time_checkedout" => strtotime($out[1])
);
}
### END USERS ###
}
# close open filehandle
pclose($fp);
# recreate license_array and expiration array
for ($t=0;$t<sizeof($feature);$t++) {
$license_array[$feature[$t]][] = array(
"num_licenses" => $num_licenses[$t],
"licenses_used" => $licenses_used[$t]
);
$expiration_array[$feature[$t]][] = array(
"vendor_daemon" => $vendor[$t],
"version" => $l_version[$t],
"expiration_date" => $exp_date[$t],
"num_licenses" => $num_licenses[$t],
"days_to_expiration" => ceil((strtotime($exp_date[$t]) - $today ) / 86400 ),
"type" => $type[$t]
);
}
$status_array = array(
"service"=>$service,
"clients"=>$clients,
"listing"=>$listing,
"version"=>$d_version,
"master"=>$master,
"msg"=>$msg
);
$master = array(
"status"=>$status_array,
"licenses"=>$license_array,
"expiration"=>$expiration_array,
"users"=>$user_array
);
return $master;
}
function get_sesi($server,$pos=0) {
global $sesictrl;
global $today;
$year = date('Y');
$x = -1;
# preload arrays
$status_array=array();
$license_array=array();
$expiration_array=array();
$user_array=array();
$service="down";
$clients="Details not available";
$listing="Expiration dates not available";
$master="";
$version="";
$msg="$server unavailable";
$fp = popen($sesictrl . " -v -i -h " . $server . " 2>&1","r");
while (!feof($fp)) {
$line = fgets ($fp,1024);
### START STATUS ###
if (preg_match("/----- SERVER (\w+).*/",trim($line),$res)) {
$master=$res[1];
$service="up";
$clients="<a href=\"details.php?listing=0&server=".$pos."\">Details</a>" ;
$listing="<a href=\"details.php?listing=1&server=".$pos."\">Expiration dates</a>" ;
$msg="";
}
if (preg_match("/sesinetd: Version (\d+\.\d+\.\d+).*/",trim($line),$res)) {
$version=$res[1];
}
### END STATUS ###
### START LICENSES/EXPIRATION ###
if ( preg_match('/(\w+):\s+(\d+)\s+\"(\w+|\w+-\w+)\s+(\d+.\d+)\s+\"\s+(\w+)\s+(\d+-\w+-\d{4}).*/i',$line,$out)) {
$x++;
$feature = $out[3];
$features[$x] = $feature;
$num_licenses[$x] = $out[2];
# if license has expired, its total used is reported as 0
# it also doesnt list how many 'free', so lets force it
if ($out[2] == 0) {
$licenses_used[$x] = "0";
}
$expiration_array[$feature][] = array(
"vendor_daemon" => $out[5],
"version" => $out[4],
"expiration_date" => $out[6],
"num_licenses" => $out[2],
"days_to_expiration" => ceil((strtotime($out[6]) - $today ) / 86400 ),
"type" => $out[1]
);
$extra[$x] = $out[1];
}
if ( preg_match('/(\d+)\s+\w+\s+\w+$/i',$line,$out3)) {
$licenses_used[$x] = $out3[1];
}
### END LICENSES/EXPIRATION ###
### START USERS ###
if ( preg_match('/.*\s(\w+\s+\d+)\s+(\d+\:\d+)/i',$line,$out2)) {
$user_array[$feature][] = array(
"line" => $out2[0],
"time_checkedout" => strtotime($out2[1] . " " . $year . " " . $out2[2])
);
}
### END USERS ###
}
//build license_array
for ($t=0;$t<sizeof($features);$t++) {
$license_array[$features[$t]][] = array(
"num_licenses" => $num_licenses[$t],
"licenses_used" => ($num_licenses[$t] - $licenses_used[$t]),
"extra" => $extra[$t],
);
}
//build status array
$status_array=array(
"service"=>$service,
"clients"=>$clients,
"listing"=>$listing,
"version"=>$version,
"master"=>$master,
"msg"=>$msg
);
//build master array
$master = array(
"status"=>$status_array,
"licenses"=>$license_array,
"expiration"=>$expiration_array,
"users"=>$user_array
);
return $master;
}
function get_tweak($server, $pos=0) {
global $tlm_server;
global $today;
# preload arrays
$status_array=array();
$license_array=array();
$expiration_array=array();
$user_array=array();
// defaults
$service="down";
$clients="Details not available";
$listing="Expiration dates not available";
$master="";
$version="";
$msg="Unable to connect to $server";
$fp = popen($tlm_server . " -s -h " . $server,"r");
while (!feof($fp)) {
$line = fgets ($fp,1024);
### START STATUS ###
if (preg_match('/RESPONSE:\s+\w+\s+\w+\s+\w+\s+\w+\s+\'(\w+)\'.*$/i',trim($line),$res)) {
$master=$res[1];
$service="up";
$clients="<a href=\"details.php?listing=0&server=".$pos."\">Details</a>" ;
$listing="<a href=\"details.php?listing=1&server=".$pos."\">Expiration dates</a>" ;
$msg="";
}
if (preg_match('/Version:\s+(\d+.\d+.\d+).*/i',trim($line),$res)) {
$version=$res[1];
}
### END STATUS ###
### START LICENSE/EXPIRATION ###
if (preg_match('/^(\w+.\d)\W\s+(\w+\s+\d+)\W+\s+\W+\s+(\d+)\s+\w+\s+\w+\W+\s+(\d+)\s+\w+\W+\s+(\d+)\s+\w+\W+\s+\w+\s+\w+\s+(\d+-\w+-\d+)/',trim($line),$license)) {
$feature = str_replace(" ","_",trim($license[2]));