1<?php 2 3/** 4 * Observium 5 * 6 * This file is part of Observium. 7 * 8 * @package observium 9 * @subpackage wmi 10 * @author Adam Armstrong <adama@observium.org> 11 * @copyright (C) 2006-2013 Adam Armstrong, (C) 2013-2019 Observium Limited 12 * 13 */ 14 15// Execute wmic using provided config variables and WQL then return output string 16// DOCME needs phpdoc block 17// TESTME needs unit testing 18function wmi_query($wql, $override = NULL, $namespace = NULL) 19{ 20 if (!isset($namespace)) 21 { 22 $namespace = $GLOBALS['config']['wmi']['namespace']; 23 } 24 25 if (isset($override) && is_array(($override))) 26 { 27 $hostname = $override['hostname']; 28 $domain = $override['domain']; 29 $username = $override['username']; 30 $password = $override['password']; 31 } 32 else 33 { 34 $hostname = $GLOBALS['device']['hostname']; 35 $domain = $GLOBALS['config']['wmi']['domain']; 36 $username = $GLOBALS['config']['wmi']['user']; 37 $password = $GLOBALS['config']['wmi']['pass']; 38 } 39 40 $options = "--user='" . $username . "' "; 41 if (empty($password)) { $options .= "--no-pass "; } else { $options .= "--password='". $password . "' "; } 42 if (!empty($domain)) { $options .= "--workgroup='". $domain . "' "; } 43 if (empty($GLOBALS['config']['wmi']['delimiter'])) { $options .= "--delimiter=## "; } else { $options .= "--delimiter=" . $GLOBALS['config']['wmi']['delimiter'] ." "; } 44 if (empty($namespace)) { $options .= "--namespace='root\CIMV2' "; } else { $options .= "--namespace='" . $namespace ."' "; } 45 if (OBS_DEBUG) { $options .= "-d2 "; } 46 $options .= "//" . $hostname; 47 48 $cmd = $GLOBALS['config']['wmic'] . " " . $options . " " . "\"".$wql."\""; 49 50 return external_exec($cmd); 51} 52 53// Import WMI string to array, remove any empty lines, find "CLASS:" in string, parse the following lines into array 54// $ret_single == TRUE will output a single dimension array only if there is one "row" of results 55// $ret_val == <WMI Property> will output the value of a single property. Only works when $ret_single == TRUE 56// Will quit if "ERROR:" is found (usually means the WMI class does not exist) 57// DOCME needs phpdoc block 58// TESTME needs unit testing 59function wmi_parse($wmi_string, $ret_single = FALSE, $ret_val = NULL) 60{ 61 62 $wmi_lines = array_filter(explode(PHP_EOL, $wmi_string), 'strlen'); 63 $wmi_class = NULL; 64 $wmi_error = NULL; 65 $wmi_properties = array(); 66 $wmi_results = array(); 67 68 foreach ($wmi_lines as $line) 69 { 70 if (preg_match('/ERROR:/', $line)) 71 { 72 $wmi_error = substr($line, strpos($line, 'ERROR:') + strlen("ERROR: ")); 73 if (OBS_DEBUG) 74 { 75 // If the error is something other than "Retrieve result data." please report it 76 switch($wmi_error) { 77 case "Retrieve result data.": 78 echo("WMI Error: Cannot connect to host or Class\n"); 79 break; 80 case "Login to remote object.": 81 echo("WMI Error: Invalid security credentials or insufficient WMI security permissions\n"); 82 break; 83 default: 84 echo("WMI Error: Please report"); 85 break; 86 } 87 } 88 return NULL; 89 } 90 if (empty($wmi_class)) 91 { 92 if (preg_match('/^CLASS:/', $line)) 93 { 94 $wmi_class = substr($line, strlen("CLASS: ")); 95 } 96 } 97 else if (empty($wmi_properties)) 98 { 99 $wmi_properties = explode($GLOBALS['config']['wmi']['delimiter'], $line); 100 } else { 101 $wmi_results[] = array_combine($wmi_properties, explode($GLOBALS['config']['wmi']['delimiter'], str_replace('(null)', '', $line))); 102 } 103 } 104 if (count($wmi_results) == 1) 105 { 106 if ($ret_single) 107 { 108 if ($ret_val) 109 { 110 return $wmi_results[0][$ret_val]; 111 } else { 112 return $wmi_results[0]; 113 } 114 } 115 } 116 117 return $wmi_results; 118} 119 120// DOCME needs phpdoc block 121// TESTME needs unit testing 122function wmi_dbAppInsert($device_id, $app) 123{ 124 $dbCheck = dbFetchRow("SELECT * FROM `applications` WHERE `device_id` = ? AND `app_type` = ? AND `app_instance` = ?", array($device_id, $app['type'], $app['instance'])); 125 126 if (empty($dbCheck)) 127 { 128 echo("Found new application '".strtoupper($app['type'])."'"); 129 if (isset($app['instance'])) 130 { 131 echo(" Instance '".$app['instance']."'"); 132 } 133 echo("\n"); 134 135 dbInsert(array('device_id' => $device_id, 'app_type' => $app['type'], 'app_instance' => $app['instance'], 'app_name' => $app['name']), 'applications'); 136 } 137 else if (empty($dbCheck['app_name']) && isset($app['name'])) 138 { 139 dbUpdate(array('app_name' => $app['name']), 'applications', "`app_id` = ?", array($dbCheck['app_id'])); 140 } 141} 142 143// EOF 144