";
$pfConfig = new PluginFusioninventoryConfig();
echo "
".__('Agent port', 'fusioninventory')." (".
__('if empty use port configured in general options', 'fusioninventory')
." ".$pfConfig->getValue('agent_port').") :
";
echo "
";
echo "";
echo "
";
echo "
";
echo "
";
echo "
";
$this->showFormButtons($options);
return true;
}
/**
* Disconnect an agent from a computer
* @params POST parameters
* @return void
*/
function disconnect($params) {
if (isset($params['computers_id']) && isset($params['id'])) {
$pfComputer = new PluginFusioninventoryInventoryComputerComputer();
$pfComputer->deleteByCriteria(['computers_id' => $params['computers_id']]);
$this->update(['id' => $params['id'], 'computers_id' => 0]);
}
}
/**
* Get agent information by device_id
*
* @global object $DB
* @param string $device_id
* @return array all data of agent from database
*/
function infoByKey($device_id) {
global $DB;
$iterator = $DB->request([
'FROM' => $this->getTable(),
'WHERE' => ['device_id' => $device_id],
'START' => 0,
'LIMIT' => 1
]);
$agent = [];
if (count($iterator)) {
$agent = $iterator->next();
}
return $agent;
}
/**
* Import token: create of update it in database
*
* @param array $arrayinventory
* @return integer id of the agent from database
*/
function importToken($arrayinventory) {
if (isset($arrayinventory['DEVICEID'])) {
$a_agent = $this->find(['device_id' => $arrayinventory['DEVICEID']], [], 1);
if (empty($a_agent)) {
$a_input = [];
if (isset($arrayinventory['TOKEN'])) {
$a_input['token'] = $arrayinventory['TOKEN'];
}
$a_input['name'] = $arrayinventory['DEVICEID'];
$a_input['device_id'] = $arrayinventory['DEVICEID'];
$a_input['entities_id'] = 0;
$a_input['last_contact'] = date("Y-m-d H:i:s");
$a_input['useragent'] = filter_input(INPUT_SERVER, "HTTP_USER_AGENT");
// Set default number of threads for network tasks to 0 to follow general setup
$a_input['threads_networkdiscovery'] = 0;
$a_input['threads_networkinventory'] = 0;
$agents_id = $this->add($a_input);
if ($agents_id) {
return $agents_id;
}
} else {
foreach ($a_agent as $data) {
$input = [];
$input['id'] = $data['id'];
if (isset($arrayinventory['TOKEN'])) {
$input['token'] = $arrayinventory['TOKEN'];
}
$input['last_contact'] = date("Y-m-d H:i:s");
$input['useragent'] = filter_input(INPUT_SERVER, "HTTP_USER_AGENT");
$this->update($input);
return $data['id'];
}
}
}
return 0;
}
/**
* Get all IP of the computer linked with this agent
*
* @return array list of IP
*/
function getIPs() {
if (!isset($this->fields['computers_id'])
|| $this->fields['computers_id'] == 0) {
trigger_error('Agent must be initialized');
}
$ip_addresses = PluginFusioninventoryToolbox::getIPforDevice('Computer', $this->fields['computers_id']);
return $ip_addresses;
}
/**
* Get the agent id linked to this computer id
*
* @param integer $computers_id id of the agent
* @return integer|false integer if found agent id, otherwise false
*/
function getAgentWithComputerid($computers_id) {
$agent = $this->find(['computers_id' => $computers_id], [], 1);
if (count($agent) == 1) {
$data = current($agent);
$this->getFromDB($data['id']);
return $data['id'];
}
return false;
}
/**
* Get the agents id of a list of computers id
*
* @param array $computer_ids list of id of computers
* @return array list of agents [id] => information of agent
*/
function getAgentsFromComputers($computer_ids = []) {
if (count($computer_ids) == 0) {
return [];
}
$agents = $this->find(['computers_id' => $computer_ids]);
return $agents;
}
/**
* Get the computer linked with this agent
*
* @return object|false return Computer object if exist, otherwise false
*/
function getAssociatedComputer() {
$computer = new Computer();
if (!isset($this->fields['id'])) {
trigger_error("Agent must be initialized!");
return false;
}
$computer->getFromDB($this->fields['computers_id']);
return $computer;
}
/**
* Link a computer with an agent
*
* @param integer $computers_id id of the computer
* @param string $device_id devide_id of the agent
* @param integer $entities_id id of the entity
* @return boolean true if successfully linked
*/
function setAgentWithComputerid($computers_id, $device_id, $entities_id) {
$a_agent = $this->find(['computers_id' => $computers_id], [], 1);
// Is this computer already linked to an agent?
$agent = array_shift($a_agent);
if (is_array($agent)) {
// relation
if ($agent['device_id'] != $device_id
|| $agent['entities_id'] != $entities_id) {
$input = [];
$input['id'] = $agent['id'];
$input['device_id'] = $device_id;
$input['entities_id'] = $entities_id;
$this->update($input);
}
// // Clean up the agent list
// $oldAgent_deviceids = $this->find(
// // computer linked to the wrong agent
// "(`computers_id`='".$computers_id."' AND `device_id` <> '".$device_id."')");
// foreach ($oldAgent_deviceids as $oldAgent) {
// $this->delete($oldAgent);
// }
$oldAgents = $this->find(
// the same device_id but linked on the wrong computer
['device_id' => $device_id, 'computers_id' => ['!=', $computers_id]]);
foreach ($oldAgents as $oldAgent) {
$input = [];
$input['id'] = $agent['id'];
$input['last_contact'] = $oldAgent['last_contact'];
$input['version'] = $oldAgent['version'];
$input['name'] = $oldAgent['name'];
$input['useragent'] = $oldAgent['useragent'];
$input['token'] = $oldAgent['token'];
$input['tag'] = $oldAgent['tag'];
$input['entities_id'] = $entities_id;
$this->update($input);
$this->delete($oldAgent);
}
return true;
} else { // This is a new computer
// Link agent with computer
$agent = $this->infoByKey($device_id);
if (isset($agent['id'])) {
$agent['computers_id'] = $computers_id;
$agent['entities_id'] = $entities_id;
$this->update($agent);
return true;
}
}
return false;
}
/**
* Display form with the remotely status of agent (available, not available,
* waiting, running...)
*
* @global array $CFG_GLPI
* @param object $computer Computer object
*/
function showRemoteStatus($computer = null) {
global $CFG_GLPI;
/**
* Check for initialized agent
*/
if (!isset($this->fields['id'])) {
return;
}
/**
* Check for initialized $computer
*/
if (is_null($computer) || !isset($computer->fields['id'])) {
return;
}
$agent_id = $this->fields['id'];
$fi_path = Plugin::getWebDir('fusioninventory');
echo "
";
}
/**
* Get the remotely status of the agent (available, not available, waiting,
* running...)
*
* @return array
*/
function getStatus() {
$url_addresses = $this->getAgentStatusURLs();
PluginFusioninventoryDisplay::disableDebug();
ob_start();
ini_set("allow_url_fopen", "1");
$ctx = stream_context_create([
'http' => [
'timeout' => 1
]
]
);
$contents="";
$url_ok = null;
$url_headers=[];
foreach ($url_addresses as $url) {
$stream = fopen($url, 'r', false, $ctx);
if ($stream) {
//$result = file_get_contents($url, FALSE, $ctx);
$contents = stream_get_contents($stream);
$url_headers[$url] = stream_get_meta_data($stream);
fclose($stream);
if ($contents !== false) {
$url_ok = $url;
break;
}
}
}
$error = ob_get_contents();
ob_end_clean();
PluginFusioninventoryDisplay::reenableusemode();
$status = [
"url_ok" => $url_ok,
"message" => ""
];
if ($contents !== "") {
$status['message'] = preg_replace("/^status: /", "", $contents);
}
if ($contents == '' AND !strstr($error, "failed to open stream: Permission denied")) {
$status['message'] = "noanswer";
}
return $status;
}
/**
* Send a request to the remotely agent to run now
*
* @return boolean true if send successfully, otherwise false
*/
function wakeUp() {
$ret = false;
PluginFusioninventoryDisplay::disableDebug();
$urls = $this->getAgentRunURLs();
$ctx = stream_context_create(['http' => ['timeout' => 2]]);
foreach ($urls as $url) {
if (!$ret) {
if (@file_get_contents($url, 0, $ctx) !== false) {
$ret = true;
break;
}
}
}
PluginFusioninventoryDisplay::reenableusemode();
return $ret;
}
/**
* Store version of each module of agent
*
* @param integer $agent_id id of the agent
* @param string $module name of the module (inventory, deploy...)
* @param string $version version of the module
*/
function setAgentVersions($agent_id, $module, $version) {
$this->getFromDB($agent_id);
$a_version = importArrayFromDB($this->fields['version']);
if (!is_array($a_version)) {
$versionTmp = $a_version;
$a_version = [];
$a_version["INVENTORY"] = $versionTmp;
}
$a_version[$module] = $version;
$input = [];
$input['id'] = $this->fields['id'];
$input['version'] = exportArrayToDB($a_version);
$this->update($input);
}
/**
* Get the version of agent (it's the same number as inventory module)
*
* @param integer $agent_id id of the agent
* @return string version of agent
*/
function getAgentVersion($agent_id) {
$this->getFromDB($agent_id);
$a_version = importArrayFromDB($this->fields['version']);
if (isset($a_version['INVENTORY'])) {
return str_replace('v', '', $a_version['INVENTORY']);
}
return '0';
}
/**
* get the agent by the device_id
*
* @param string $device_id the device_id sent by the agent
* @return array|false agent information if found, otherwise false
*/
static function getByDeviceID($device_id) {
$agents = getAllDataFromTable(
'glpi_plugin_fusioninventory_agents',
[
'device_id' => $device_id,
'lock' => 0
]
);
if (!empty($agents)) {
return array_pop($agents);
} else {
return false;
}
}
/**
* Get / generate the URLs to communicate with current agent
*
* @return array list of HTTP URL used to contact the agent
*/
public function getAgentBaseURLs() {
$config = new PluginFusioninventoryConfig();
$port = $config->getValue('agent_port');
$url_addresses = [];
if (isset($this->fields['id'])) {
$computer = $this->getAssociatedComputer();
if ($this->fields['agent_port'] != ''
&& is_numeric($this->fields['agent_port'])) {
$port = $this->fields['agent_port'];
}
if ($computer->fields["name"] && $computer->fields["name"] != "localhost") {
array_push($url_addresses, "http://".$computer->fields["name"].
":".$port);
$ditem = new Domain_Item();
if ($ditem->getFromDBByCrit(['itemtype' => 'Computer', 'items_id' => $computer->fields['id']])) {
$domain = new Domain();
$domain->getFromDB($ditem->fields['domains_id']);
array_push($url_addresses, "http://".
$computer->fields["name"].'.'.
$domain->fields["name"].
":".$port);
}
}
}
// Guess the machine name from the DEVICEID,
// useful when Windows domain != DNS domain
$stack = [];
if (preg_match('/(\S+)-\d{4}-\d{2}-\d{2}-\d{2}-\d{2}-\d{2}$/',
$this->fields['name'],
$stack)) {
array_push($url_addresses, "http://".$stack[1].":".$port);
}
$ip_addresses = $this->getIPs();
foreach ($ip_addresses as $ip_address) {
if ($ip_address != '') {
array_push($url_addresses, "http://".$ip_address.":".$port);
}
}
return $url_addresses;
}
/**
* Get the URLs used to get the status of the agent
*
* @return array list of HTTP URL to get the agent's state
*/
public function getAgentStatusURLs() {
$ret = [];
foreach ($this->getAgentBaseURLs() as $url) {
array_push($ret, $url."/status");
}
return $ret;
}
/**
* Get the URLs used to wake up the agent
*
* @return array liste of HTTP URL to ask the agent to wake up
*/
public function getAgentRunURLs() {
$ret = [];
foreach ($this->getAgentBaseURLs() as $url) {
array_push($ret, $url."/now/".$this->fields['token']);
}
return $ret;
}
/**
* Display configuration form of agent
*/
static function showConfig() {
echo "
";
echo "
";
echo "
";
echo __('Informations for agent configuration', 'fusioninventory');
echo "
";
}
/**
* Disable data to put in glpi_logs because don't want to write all these
* very often changes
*/
function pre_updateInDB() {
if (isset($this->oldvalues['version'])
AND $this->input['version'] == $this->oldvalues['version']) {
$key = array_search('version', $this->updates);
unset($this->oldvalues['version']);
}
if (isset($this->oldvalues['last_contact'])) {
$key = array_search('last_contact', $this->updates);
unset($this->oldvalues['last_contact']);
}
if (isset($this->oldvalues['token'])) {
$key = array_search('token', $this->updates);
unset($this->oldvalues['token']);
}
}
/**
* Display agent information for a computer
*
* @param Computer the computer
* @param integer $colspan the number of columns of the form (2 by default)
*/
function showInfoForComputer(Computer $computer, $colspan = 2) {
if ($this->getAgentWithComputerid($computer->getID())) {
echo '