Source for file wsus_getinfo.php
Documentation is available at wsus_getinfo.php
* LICENSE: This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation.
* @author Hector Ortiz (FreeNAC Core Team)
* @copyright 2006 FreeNAC
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License Version 2
* @link http://www.freenac.net
* Retrieves information from the WSUS database and stored in local mysql
* Beta test with Wsus V2.???
* Stable release v2.2 testted with Wusus version 3.???
# Php weirdness: change to script dir, then look for includes
* Load settings and commong functions
require_once "./funcs.inc.php";
$logger->setLogToStdOut();
* Simple parsing of command line parameters
$logger->setDebugLevel(1);
$logger->setDebugLevel(2);
case '-s':
$output =
FALSE;
$timestamp=
date('Y-m-d H:i:s');
message("Program run on $timestamp",1);
$logger->logit( "Usage: wsus_getinfo [-h][-v[v]][-s]\n");
$logger->logit( "\t-h\tShow this help screen\n");
$logger->logit( "\t-v\tDebug level 1 enabled (output goes to stdout & syslog)\n");
$logger->logit( "\t-vv\tDebug level 1 & 2 enabled (output goes to stdout & syslog)\n");
$logger->logit( "\t-s\tSupress messages to standard output and redirect them to syslog\n");
* Connect to the WSUS server
global $conf,$wsus_dbuser,$wsus_dbpass;
message("Connect to ".
$conf->wsus_dbalias.
" ".
$conf->wsus_db,1);
$msconnect =
mssql_connect($conf->wsus_dbalias, $wsus_dbuser, $wsus_dbpass);
message("Cannot connect to WSUS server ".
$conf->wsus_dbalias.
":" .
mssql_get_last_message(),0);
$d =
mssql_select_db($conf->wsus_db, $msconnect);
message("Couldn't open database ".
$conf->wsus_db.
" ".
mssql_get_last_message(),0);
* Ensures that $string is mysql safe
* Executes query and displays error message if any
* This function converts the datetime retrieved from MSSQL into MySQL datetime format
$date=
$date_array['year'].
'-';
$date_array['mon'] <
10 ?
$date.=
'0'.
$date_array['mon'].
'-' :
$date.=
$date_array['mon'].
'-';
$date_array['mday'] <
10 ?
$date.=
'0'.
$date_array['mday'].
' ' :
$date.=
$date_array['mday'].
' ';
$date_array['hours'] <
10 ?
$date.=
'0'.
$date_array['hours'].
':' :
$date.=
$date_array['hours'].
':';
$date_array['minutes'] <
10 ?
$date.=
'0'.
$date_array['minutes'].
':' :
$date.=
$date_array['minutes'].
':';
$date_array['seconds'] <
10 ?
$date.=
'0'.
$date_array['seconds'] :
$date.=
$date_array['seconds'];
* Dumps the tbComputerTarget table into our computertarget tabl
message("Function wsus_dump_computertarget",1);
#This query is for WSUS 2.X
#$query="select TargetID,IPAddress,FullDomainName,OSMajorVersion,OSMinorVersion,OSBuildNumber,OSServicePackMajorNumber,OSServicePackMinorNumber,OSLocale,ComputerMake,ComputerModel,BiosVersion,BiosName,BiosReleaseDate,ProcessorArchitecture from tbComputerTarget";
#This query is for WSUS 3.0
$query=
"select ct.TargetID, ct.IPAddress, ct.FullDomainName, td.OSMajorVersion, td.OSMinorVersion, td.OSBuildNumber, td.OSServicePackMajorNumber, td.OSServicePackMinorNumber, td.OSLocale, td.ComputerMake, td.ComputerModel, td.BiosVersion, td.BiosName, td.BiosReleaseDate, td.ProcessorArchitecture from tbComputerTarget as ct, tbcomputertargetdetail as td where ct.targetID=td.targetID";
$old_date=
$row[BiosReleaseDate];
$temp=
explode('.',$row[FullDomainName]);
$row[FullDomainName]=
$temp[0];
message("Converted $old_date into ".
$row[BiosReleaseDate].
" for ".
$row[FullDomainName],1);
$row[OSLocale]=
substr($row[OSLocale],0,2);
$osid=
wsus_get_osid($row[OSMajorVersion],$row[OSMinorVersion],$row[OSBuildNumber],$row[OSServicePackMajorNumber],$row[OSServicePackMinorNumber],$row[ProcessorArchitecture]);
$query=
sprintf("insert into nac_wsuscomputertarget (TargetID, IPAddress, FullDomainName, OSid, OSLocale, ComputerMake, ComputerModel, BiosVersion, BiosName, BiosReleaseDate) values ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s') on duplicate key update datetime=NOW();", validate($row[TargetID]), validate($row[IPAddress]), validate($row[FullDomainName]), validate($osid), validate($row[OSLocale]), validate($row[ComputerMake]), validate($row[ComputerModel]), validate($row[BiosVersion]), validate($row[BiosName]), validate($row[BiosReleaseDate]));
* Dumps table tbOSMap into our osmap table
message("Function wsus_dump_osmap",1);
$query=
"select * from tbOSMap";
$query=
'delete from nac_wsusosmap'; //Delete everything we previosuly had.
$query=
sprintf("insert into nac_wsusosmap values ('%s','%s','%s','%s','%s','%s','%s','%s','%s');", $row[OSid], $row[OSMajorVersion], $row[OSMinorVersion], $row[OSBuildNumber], $row[OSServicePackMajorNumber], $row[OSServicePackMinorNumber], $row[ProcessorArchitecture],validate($row[OSShortName]),validate($row[OSLongName]));
function wsus_get_osid($majv,$minv,$build,$spmaj,$spmin,$processor)
message("Function wsus_getosid",1);
$query=
"select pid from nac_wsusprocessor where ProcessorArchitecture='$processor';";
$query=
sprintf("select osid from tbosmap where osmajorversion='%s' and osminorversion='%s' and osbuildnumber='%s' and osservicepackmajornumber='%s' and osservicepackminornumber='%s' and processorarchitecture='%s'",$majv,$minv,$build,$spmaj,$spmin,$my_result['pid']);
* Retrieves the installed and needed updates for computer identified by id
message("Function wsus_get_updates_per_computer for $id",1);
#$query="select SummarizationState, UpdateID, LastChangeTime, LastRefreshTime from tbUpdateStatusPerComputer where TargetID='$id' and SummarizationState=4 order by id asc"; #Installed updates
$query=
"select SummarizationState, LocalUpdateID, LastChangeTime, LastRefreshTime from tbUpdateStatusPerComputer where TargetID='$id' and SummarizationState=4 order by LocalUpdateID asc"; #Installed updates
$query=
"delete from nac_wsusupdatestatuspercomputer where TargetID='$id' and SummarizationState='4';";
foreach($installed as $row)
#$query=sprintf("insert into nac_wsusupdatestatuspercomputer values('%s','%s','%s','%s','%s');",validate($row[SummarizationState]),validate(mssql_guid_string($row[UpdateID])),validate($id),validate(convert_date($row[LastChangeTime])),validate(convert_date($row[LastRefreshTime])));
#$query="select SummarizationState, UpdateID, LastChangeTime, LastRefreshTime from tbUpdateStatusPerComputer where TargetID='$id' and SummarizationState='6' order by id asc";#Needed updates
$query=
"select SummarizationState, LocalUpdateID, LastChangeTime, LastRefreshTime from tbUpdateStatusPerComputer where TargetID='$id' and SummarizationState=4 order by LocalUpdateID asc"; #Installed updates
$query=
"delete from nac_wsusupdatestatuspercomputer where targetID='$id' and SummarizationState='6';";
#$query=sprintf("insert into nac_wsusupdatestatuspercomputer values('%s','%s','%s','%s','%s');",validate($row[SummarizationState]),validate(mssql_guid_string($row[UpdateID])),validate($id),validate(convert_date($row[LastChangeTime])),validate(convert_date($row[LastRefreshTime])));
$query=
"select LastSyncTime from tbcomputertarget where TargetID='$id';";
$query=
"update nac_wsuscomputertarget set LastSyncTime='$lastsynctime' where targetid='$id';";
* This one retrieves the installed and needed updates for every computer
message("Function wsus_dump_updates_for_computers",1);
$query=
"select TargetID from nac_wsuscomputertarget;";
* Connect data from WSUS with the data stored in the systems table
$query=
"select id,name from systems";
if ((!empty($row['name']))&&
(strcasecmp($row['name'],'unknown')!=
0))
$query=
"update nac_wsuscomputertarget set sid=".
$row['id'].
" where FullDomainName like '".
$row['name'].
"'";
message("Updated patch information for host {$row['name']}",0);
message("Possible duplicates in database for host {$row['name']}",0);
* Get a list of updates available on the server
message("Function wsus_dump_updates",1);
$query=
"select u.localupdateid as LocalID,u.updateid as UpdateID,pre.title as Title,kb.kbarticleid as Article from tbprecomputedlocalizedproperty pre, tbkbarticleforrevision kb, tbupdate u where pre.updateid=u.updateid and pre.revisionid=kb.revisionid and pre.shortlanguage='$wsus_language' order by (kb.revisionid) asc";
/*$query='delete from nac_wsusupdate;';
message("Executing: ".$query,2);
$updateid=
mssql_guid_string($row[UpdateID]);
$logger->setLogToStdOut();
$logger->logit("$string");
$logger->setLogToStdOut(false);
if ($logger->getDebugLevel())
$logger->debug($string,1);
$logger->setLogToStdOut();
$logger->logit("$string");
if ($logger->getDebugLevel())
$logger->debug($string,2);
$logger->setLogToStdOut();
$logger->logit("$string");
$enabled=
v_sql_1_select("select value from config where name='wsus_enabled'");
message("Dumping remote table tbOSmap into our osmap table.",0);
message("Dumping patches available on the server",0);
message("Dumping remote table tbComputerTarget into our computertarget table.",0);
message("Retrieving patch information for computers.",0);
message("Connecting information from WSUS to the systems table.",0);
logit("WSUS synchronization was successful.");
log2db('info',"WSUS synchronization was successful.");
else message("Function connect_data failed.",0);
else message("Function wsus_dump_updates_for_computers failed.",0);
else message("Function wsus_dump_computertarget failed.",0);
else message("Function wsus_dump_updates failed.",0);
else message("Function wsus_dump_osmap failed.",0);
logit("WSUS synchronization failed.");
log2db('err',"WSUS synchronization failed.");
message("This function is not enabled",0);