getMessage()); } } ################################################################ # Get current date and time ################################################################ $date = date('Y-m-d'); $year = date('Y'); $time = date('H:i') . ":00"; # Cycle the number of license servers, defined by the config.php for ( $i = 0 ; $i < sizeof($servers) ; $i++ ) { ################################################################ # Get current license report via the lmutil lmstat command ################################################################ $fp = popen($lmutil_loc . " lmstat -a -c " . $servers[$i], "r"); # cylce the report until the end of file while ( !feof ($fp) ) { # get one line from the report $line = fgets ($fp, 1024); # Look for features in the output. You will see stuff like # Users of Allegro_Viewer: (Total of 5 licenses available if ( eregi("^(Users of) (.*)",$line, $out ) ) { if ( eregi("(Total of) (.*) (license[s]? issued; Total of) (.*) (license[s]? in use)", $line, $items ) ) { # get the full feature name $cell_feature_temp = substr($out[2],0,strpos($out[2],":")); # store the bundle name and feature name first time. Asume there is no bundle $cell_bundle = ""; $cell_feature = $cell_feature_temp; # Check if feature is part of a bundle # Compare with bundle array. maybe defined in the config.php # $license_bundle[] = "NX12100N"; # $license_bundle[] = "NX13100N"; # $license_bundle[] = "NX13500"; for ( $j = 0 ; $j < sizeof($license_bundle) ; $j++ ) { # check if bundle ID is found in feature name $bundlepos = strpos($cell_feature_temp, $license_bundle[$j]); If ($bundlepos === 0) { # bundle string was found at the beginning # Check if the feature is not the bundle core. E.g. for each bundle a single feature licens, containing the bundel number only, exists if ( strlen($cell_feature_temp) === strlen($license_bundle[$j])) { $cell_feature = $license_bundle[$j]; } else { $cell_feature = substr($cell_feature_temp, (strlen($license_bundle[$j])+1)); } # store the bundle name $cell_bundle = $license_bundle[$j]; } } # get the number of used licenses (is not used in databse) $cell_licenses_used = $items[4]; # unset the generated tokens unset($out); unset($items); } } # Look for users of features in the output. You will see stuff like # " start " if ( preg_match("/\bstart\b/i",$line) ) { #if ( eregi("(Total of) (.*) (license[s]? issued; Total of) (.*) (license[s]? in use)", $line, $items ) ) { # I fone user line was found split the the line into tokens, seperated by a space $out = preg_split("/[\s]+/",$line); # the first token is the users name in 2Tier access (e.g. NX direct or tc8UA) $cell_user = $out[1]; # the second token is the client workstation, which was requesting the license (In a 4Tier mode on Tc2005sr1 this will be the pool manager server machine) $cell_clientmachine = $out[2]; # In a 4Tier mode on tc2005sr1 the user is listed as port on the server (e.g. servername0.0mustermann,max # split this token by the "0.0" segment $cell_client_temp = preg_split("/[0][.][0]/", $out[3]); # Use the right token as representation for the user in 4Tier mode $cell_client = $cell_client_temp[1]; # Now use the token behind the " start " term $line4 = split(", start ", $line); # split this token again by spaces $line5 = preg_split("/[\s]+/",$line4[1]); # The day name can be used in addition. But in this case it is not used $cell_dayname = $line5[0]; #echo "line5[0]=" . $line5[0] . "\r\n"; #echo "line5[1]=" . $line5[1] . "\r\n"; #echo "line5[2]=" . $line5[2] . "\r\n"; # Get the date tokens (month and day) $cell_date_temp = preg_split("[/]",$line5[1]); $cell_date_temp_month = $cell_date_temp[0]; # If the month is only at a length of one digit populate it to two digits e.g. 08 If ( strlen($cell_date_temp_month) === 1) { $cell_date_temp_month = "0" . $cell_date_temp_month; } # Get the day token $cell_date_temp_day = $cell_date_temp[1]; # If the day token is at a length of one digit extend it to two digits e.g. 05 If ( strlen($cell_date_temp_day) === 1) { $cell_date_temp_day = "0" . $cell_date_temp_day; } # Re - create a full date format for MySQL $cell_date = $year . "-". $cell_date_temp_month . "-" . $cell_date_temp_day; # get the time tokens $cell_time_temp = preg_split("/[:]/",$line5[2]); # process the hours and minutes similar to the day and month tokens $cell_time_temp_hour = $cell_time_temp[0]; If ( strlen($cell_time_temp_hour) === 1) { $cell_time_temp_hour = "0" . $cell_time_temp_hour; } $cell_time_temp_minute = $cell_time_temp[1]; If ( strlen($cell_time_temp_minute) === 1) { $cell_time_temp_minute = "0" . $cell_time_temp_minute; } # Re - Create a full Time Format for MySQL $cell_time = $cell_time_temp_hour . ":" . $cell_time_temp_minute . ":00"; # Process the time tokens again and calulate the time difference $line3 = split(", start ", $line); # Get current UNIX time stamp $now = time (); preg_match("/(.+?) (.+?) (\d+):(\d+)/i", $line3[1], $line2); # Convert the date and time ie 12/5 9:57 to UNIX time stamp $time_checkedout = strtotime ($line2[2] . " " . $line2[3] . ":" . $line2[4]); $time_difference = ""; ################################################################ # This is what I am not very clear on but let's say a license has been # checked out on 12/31 and today is 1/2. It is unclear to me whether # strotime will handle the conversion correctly ie. 12/31 will actually # be 12/31 of previous year and not the current. Thus I will make a # little check here. Will just append the previous year if now is less # then time_checked_out ################################################################ #echo "UNIX Now =" .$now ."\r\n"; #echo "UNIX Checkout=" . $time_checkedout ."\r\n"; if ( $now < $time_checkedout ){ $time_checkedout = strtotime ($line2[2] . "/" . (date("Y") - 1) . " " . $line2[3]); }else { # Get the time difference $t = new timespan( $now, $time_checkedout ); # Format the date string if ( $t->years > 0) $time_difference .= $t->years . " years(s), "; if ( $t->months > 0) $time_difference .= $t->months . " month(s), "; if ( $t->weeks > 0) $time_difference .= $t->weeks . " week(s), "; if ( $t->days > 0) $time_difference .= " " . $t->days . " day(s), "; if ( $t->hours > 0) $time_difference .= " " . $t->hours . " hour(s), "; $time_difference .= $t->minutes . " minute(s)"; } #if ( $t->minutes == 0) { # $duration_total_minutes = 1; #} else { $duration_total_minutes = $t->minutes; #} $duration_total_minutes = $duration_total_minutes + ( $t->hours * 60 ); $duration_total_minutes = $duration_total_minutes + ( $t->days * 1440 ); $duration_total_minutes = $duration_total_minutes + ( $t->weeks * 10080 ); unset($out); unset($items); #} # Store the array $license_array[] = array ( "server" => $servers[$i], "bundle" => $cell_bundle, "feature" => $cell_feature, "licenses_used" => $cell_licenses_used, "user" => $cell_user, "machine" => $cell_clientmachine, "client" => $cell_client, "start_date" => $cell_date, "start_time" => $cell_time, "duration" => $time_difference, "duration_years" => $t->years, "duration_months" => $t->months, "duration_weeks" => $t->weeks, "duration_days" => $t->days, "duration_hours" => $t->hours, "duration_minutes" => $t->minutes, "duration_total_minutes" => $duration_total_minutes ); } } # close the output for one server pclose($fp); # The following block can be used to check the array in a debug session #for ( $j = 0 ; $j < sizeof($license_array) ; $j++ ) { #echo "License Server:=" . $license_array[$j]["server"]."\r\n"; #echo "Date=" . $date . " Time=" . $time . "\r\n"; #echo "Bundle:=" .$license_array[$j]["bundle"]. " Feature:=". $license_array[$j]["feature"] . " Licenses_Used:=". $license_array[$j]["licenses_used"] . "\r\n"; #echo "User:=" . $license_array[$j]["user"] . " Machine:=" . $license_array[$j]["machine"] . " Client:=" . $license_array[$j]["client"] . "\r\n"; #echo "Start Date:=" . $license_array[$j]["start_date"]. " Start Time:=" . $license_array[$j]["start_time"]. " Duration:=" . $license_array[$j]["duration"]."\r\n"; #echo "Differenz Years:=" . $license_array[$j]["duration_years"]. " Months:=" . $license_array[$j]["duration_months"]. " weeks:=" . $license_array[$j]["duration_weeks"]. " days:=" . $license_array[$j]["duration_days"]."\r\n"; #echo "Differenz Hours:=" . $license_array[$j]["duration_hours"]. " Minutes:=" . $license_array[$j]["duration_minutes"]."\r\n"; #echo "Differenz Total Minutes:=" . $license_array[$j]["duration_total_minutes"]."\r\n"; #echo "\r\n"; #} # $conn->debug=true; if ( isset($license_array) && is_array ($license_array) ) { for ( $j = 0 ; $j < sizeof($license_array) ; $j++ ) { ############################################################################ # Only insert into the database if DB settings are set ############################################################################ if ( isset($db_hostname) && isset($db_username) && isset($db_password) ) { ################################################################ # check if a dataset exists for the current feature - user combination ################################################################ $sql = "SELECT start , totalminutes, lastminutes FROM license_used WHERE "; $sql = $sql . "server='". $license_array[$j]["server"] . "' AND "; $sql = $sql . "bundle='". $license_array[$j]["bundle"] . "' AND "; $sql = $sql . "feature='". $license_array[$j]["feature"] . "' AND "; $sql = $sql . "user='". $license_array[$j]["user"] . "' AND "; $sql = $sql . "machine='". $license_array[$j]["machine"] . "' AND "; $sql = $sql . "client='". $license_array[$j]["client"] . "' AND "; $sql = $sql . "date='". $license_array[$j]["start_date"] . "'"; $recordset2 = $db->query($sql); $num_rows2 = $recordset2->numRows(); # If the there exists no record in the database create a new one If ( $num_rows2 === 0 ) { #echo "No record found => Insert New Record\r\n"; $sql = "INSERT INTO license_used "; $sql = $sql . "(server,bundle,feature,user,machine,client,date,start,totalminutes, lastminutes) VALUES ("; $sql = $sql . "'". $license_array[$j]["server"] . "',"; $sql = $sql . "'". $license_array[$j]["bundle"] . "',"; $sql = $sql . "'". $license_array[$j]["feature"] . "',"; $sql = $sql . "'". $license_array[$j]["user"] . "',"; $sql = $sql . "'". $license_array[$j]["machine"] . "',"; $sql = $sql . "'". $license_array[$j]["client"] . "',"; $sql = $sql . "'". $license_array[$j]["start_date"] . "',"; $sql = $sql . "'". $license_array[$j]["start_time"] . "',"; $sql = $sql . "'". $license_array[$j]["duration_total_minutes"] . "',"; $sql = $sql . "'". $license_array[$j]["duration_total_minutes"] . "')"; $recordset = $db->query($sql); if (DB::isError($recordset)) { die ($recordset->getMessage()); } } # If there exists one record calculate the new values and update the record If ( $num_rows2 === 1 ) { #echo "One record found => Get stored start time and compare it to the calculated one\r\n"; # get start time and stored duration (total and since last stored start time) from database while ($row2 = $recordset2->fetchRow()){ $starttimestored = $row2[0]; $totalminutesstored = $row2[1]; $lastminutesstored = $row2[2]; } # compare the start time values and calulate the total duration if ( $starttimestored === $license_array[$j]["start_time"] ) { #echo "The stored start time is exactly the one of the current report\r\n"; $newstarttime = $starttimestored; $newlastminutes = $license_array[$j]["duration_total_minutes"]; $newtotalminutes = $totalminutesstored + ( $newlastminutes - $lastminutesstored); } else { #echo "The stored start time differs from the current report!!\r\n"; $newstarttime = $license_array[$j]["start_time"]; $newlastminutes = $license_array[$j]["duration_total_minutes"]; $newtotalminutes = $totalminutesstored + $newlastminutes; } # update the dataset $sql = "UPDATE license_used SET "; $sql = $sql . "start='".$newstarttime."', "; $sql = $sql . "lastminutes='".$newlastminutes."', "; $sql = $sql . "totalminutes='". $newtotalminutes ."' WHERE "; $sql = $sql . "server='". $license_array[$j]["server"] . "' AND "; $sql = $sql . "bundle='". $license_array[$j]["bundle"] . "' AND "; $sql = $sql . "feature='". $license_array[$j]["feature"] . "' AND "; $sql = $sql . "user='". $license_array[$j]["user"] . "' AND "; $sql = $sql . "machine='". $license_array[$j]["machine"] . "' AND "; $sql = $sql . "client='". $license_array[$j]["client"] . "' AND "; $sql = $sql . "date='". $license_array[$j]["start_date"] . "'"; $recordset = $db->query($sql); if (DB::isError($recordset)) { die ($recordset->getMessage()); } } # If there are multiple records for the user - feature - server combination there is ana error in the storage logic. Up to now it is not evaluated If ( $num_rows2 > 1 ) { echo "Multiple records found\r\n"; } } ########################################################################## # Insert into the RRD file if feature is being monitored ########################################################################## if ( isset($rrdtool_bin) && is_executable($rrdtool_bin) && in_array($license_array[$j]["feature"], $license_feature) ) { $feature = strtolower($license_array[$j]["feature"]); $filename = $rrd_dir . "/" . $feature . ".rrd"; insert_into_rrd($rrdtool_bin, $filename, $license_array[$j]["licenses_used"]); } } unset($license_array); } } ########################################################################## # Disconnect connection if using DB ########################################################################## if ( isset($db_hostname) && isset($db_username) && isset($db_password) ) { $db->disconnect(); } ?>