namespace = escapeshellarg('root\CIMV2'); $this->host = null; // $this->class = 'Win32_ComputerSystem'; // $this->attributes = array('*'); $this->credential = '/etc/cacti/cactiwmi.pw'; $this->filterkey = null; $this->filterval = null; $this->wmic = '/usr/local/bin/wmic'; } function setFilter($filterkey, $filterval) { $this->filterkey = $filterkey; $this->filterval = $filterval; } function execQuery($class, $attributes) { // build columns we want foreach ($attributes as $attr) { $columns .= $attr.','; }; // strip off the last , character as we don't want it $columns = substr($columns,0,-1); $wmiquery = 'SELECT '.$columns.' FROM '.$class; // basic query built if (isset($this->filterkey)) { $wmiquery = $wmiquery.' WHERE '.$this->filterkey.'='.escapeshellarg($this->filterval); // if the query has a filter argument add it in }; $wmiquery = '"'.$wmiquery.'"'; // encapsulate the query in " " $wmiexec = $this->wmic.' --namespace='.$this->namespace.' --authentication-file='.$this->credential.' //'.$this->host.' '.$wmiquery. ' 2>/dev/null'; // setup the query to be run and hide error messages // echo $wmiexec; exec($wmiexec,$wmiout,$execstatus); // execute the query and store output in $wmiout and return code in $execstatus $wmi_count = count($wmiout); // count the number of lines returned from wmic, saves recouting later if ($wmi_count > 0) { $names = explode('|',$wmiout[1]); // build the names list to dymanically output it $out = array(); for($i=2;$i<$wmi_count;$i++) { // dynamically manage the key:value pairs $data = explode('|',$wmiout[$i]); $j=0; foreach($data as $item) { // build the data part of the array $tmp[$names[$j++]] = trim($item); }; // and add the data to the final output array $out[] = $tmp; }; }; return $out; } } ?>