2020-10-06 15:48:12 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Event entity class.
|
|
|
|
*
|
|
|
|
* @category Class
|
|
|
|
* @package Pandora FMS
|
|
|
|
* @subpackage OpenSource
|
|
|
|
* @version 1.0.0
|
|
|
|
* @license See below
|
|
|
|
*
|
|
|
|
* ______ ___ _______ _______ ________
|
|
|
|
* | __ \.-----.--.--.--| |.-----.----.-----. | ___| | | __|
|
|
|
|
* | __/| _ | | _ || _ | _| _ | | ___| |__ |
|
|
|
|
* |___| |___._|__|__|_____||_____|__| |___._| |___| |__|_|__|_______|
|
|
|
|
*
|
|
|
|
* ============================================================================
|
2020-11-27 13:52:35 +01:00
|
|
|
* Copyright (c) 2005-2021 Artica Soluciones Tecnologicas
|
2020-10-06 15:48:12 +02:00
|
|
|
* Please see http://pandorafms.org for full contribution list
|
|
|
|
* 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 for version 2.
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* ============================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Begin.
|
|
|
|
namespace PandoraFMS;
|
|
|
|
|
|
|
|
global $config;
|
|
|
|
require_once $config['homedir'].'/include/functions_events.php';
|
|
|
|
|
|
|
|
/**
|
2020-12-10 17:53:49 +01:00
|
|
|
* PandoraFMS event entity.
|
2020-10-06 15:48:12 +02:00
|
|
|
*/
|
|
|
|
class Event extends Entity
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
2020-12-10 17:53:49 +01:00
|
|
|
* Agent related to this event.
|
2020-10-06 15:48:12 +02:00
|
|
|
*
|
2020-12-10 17:53:49 +01:00
|
|
|
* @var \PandoraFMS\Agent
|
2020-10-06 15:48:12 +02:00
|
|
|
*/
|
2020-12-10 17:53:49 +01:00
|
|
|
private $linkedAgent;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module related to this event.
|
|
|
|
*
|
|
|
|
* @var \PandoraFMS\Module
|
|
|
|
*/
|
|
|
|
private $linkedModule;
|
2020-10-06 15:48:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-12-10 17:53:49 +01:00
|
|
|
* Builds a PandoraFMS\Event object from given event id.
|
2020-10-06 15:48:12 +02:00
|
|
|
*
|
2020-12-10 17:53:49 +01:00
|
|
|
* @param integer $event_id Event Id.
|
2020-10-06 15:48:12 +02:00
|
|
|
*/
|
2020-12-10 17:53:49 +01:00
|
|
|
public function __construct(?int $event_id=null)
|
2020-10-06 15:48:12 +02:00
|
|
|
{
|
2021-04-15 12:00:00 +02:00
|
|
|
$this->table = ((bool) \is_metaconsole() === true) ? 'tmetaconsole_event' : 'tevento';
|
2020-10-06 15:48:12 +02:00
|
|
|
|
2020-12-10 17:53:49 +01:00
|
|
|
if ($event_id === 0) {
|
2021-04-15 12:00:00 +02:00
|
|
|
parent::__construct($this->table);
|
2020-12-10 17:53:49 +01:00
|
|
|
} else if (is_numeric($event_id) === true) {
|
2021-04-15 12:00:00 +02:00
|
|
|
parent::__construct($this->table, ['id_evento' => $event_id]);
|
2020-10-06 15:48:12 +02:00
|
|
|
} else {
|
|
|
|
// Empty skel.
|
2021-04-15 12:00:00 +02:00
|
|
|
parent::__construct($this->table);
|
2020-10-06 15:48:12 +02:00
|
|
|
}
|
|
|
|
|
2021-02-03 14:21:59 +01:00
|
|
|
try {
|
|
|
|
if ((bool) \is_metaconsole() === true
|
|
|
|
&& $this->server_id() !== null
|
|
|
|
) {
|
|
|
|
$this->nodeId = $this->server_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->connectNode();
|
|
|
|
|
|
|
|
if ($this->id_agente() !== null) {
|
|
|
|
$this->linkedAgent = new Agent((int) $this->id_agente());
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->id_agentmodule() !== null) {
|
|
|
|
$this->linkedModule = new Module((int) $this->id_agentmodule());
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
// Do not link items if failed to find them.
|
|
|
|
$this->restoreConnection();
|
2020-12-10 17:53:49 +01:00
|
|
|
}
|
|
|
|
|
2021-02-03 14:21:59 +01:00
|
|
|
// Restore if needed.
|
|
|
|
$this->restoreConnection();
|
2020-12-10 17:53:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get/set linked agent.
|
|
|
|
*
|
|
|
|
* @param Agent|null $agent New agent to link.
|
|
|
|
*
|
|
|
|
* @return Agent|null Agent or null if set operation.
|
|
|
|
*/
|
|
|
|
public function agent(?Agent $agent=null) : ?Agent
|
|
|
|
{
|
|
|
|
if ($agent === null) {
|
|
|
|
return $this->linkedAgent;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->linkedAgent = $agent;
|
|
|
|
$this->id_agentmodule($agent->id_agentmodule());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get/set linked agent.
|
|
|
|
*
|
|
|
|
* @param Module|null $module New module to link.
|
|
|
|
*
|
|
|
|
* @return Module|null module or null if set operation.
|
|
|
|
*/
|
|
|
|
public function module(?Module $module=null) : ?Module
|
|
|
|
{
|
|
|
|
if ($module === null) {
|
|
|
|
return $this->linkedModule;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->linkedModule = $module;
|
|
|
|
$this->id_agentmodule($module->id_agentmodule());
|
2020-10-06 15:48:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves all events matching given filters.
|
|
|
|
*
|
|
|
|
* @param array $fields Fields to retrieve.
|
|
|
|
* @param array $filter Filter.
|
|
|
|
* @param integer $offset Offset.
|
|
|
|
* @param integer $limit Limit.
|
|
|
|
* @param string $order Order (asc or desc).
|
|
|
|
* @param string $sort_field Sort field.
|
|
|
|
* @param boolean $history Search history.
|
|
|
|
* @param boolean $return_sql Return sql or execute it.
|
|
|
|
* @param string $having Having.
|
|
|
|
*
|
2020-12-10 17:53:49 +01:00
|
|
|
* @return array|string|false Found events or SQL query or error.
|
|
|
|
* @throws \Exception On error.
|
2020-10-06 15:48:12 +02:00
|
|
|
*/
|
|
|
|
public static function search(
|
|
|
|
array $fields,
|
|
|
|
array $filter,
|
|
|
|
?int $offset=null,
|
|
|
|
?int $limit=null,
|
|
|
|
?string $order=null,
|
|
|
|
?string $sort_field=null,
|
|
|
|
bool $history=false,
|
|
|
|
bool $return_sql=false,
|
|
|
|
string $having=''
|
|
|
|
):array {
|
|
|
|
return \events_get_all(
|
|
|
|
$fields,
|
|
|
|
$filter,
|
|
|
|
$offset,
|
|
|
|
$limit,
|
|
|
|
$order,
|
|
|
|
$sort_field,
|
|
|
|
$history,
|
|
|
|
$return_sql,
|
|
|
|
$having
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves current group definition to database.
|
|
|
|
*
|
|
|
|
* @return mixed Affected rows of false in case of error.
|
|
|
|
* @throws \Exception On error.
|
|
|
|
*/
|
|
|
|
public function save()
|
|
|
|
{
|
2021-04-15 12:00:00 +02:00
|
|
|
$values = $this->fields;
|
|
|
|
// Clean null fields.
|
|
|
|
foreach ($values as $k => $v) {
|
|
|
|
if ($v === null) {
|
|
|
|
unset($values[$k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 15:48:12 +02:00
|
|
|
if ($this->id_evento === null) {
|
|
|
|
// New.
|
|
|
|
return db_process_sql_insert(
|
2021-04-15 12:00:00 +02:00
|
|
|
$this->table,
|
|
|
|
$values
|
2020-10-06 15:48:12 +02:00
|
|
|
);
|
|
|
|
} else if ($this->fields['id_evento'] > 0) {
|
|
|
|
// Update.
|
|
|
|
return db_process_sql_update(
|
2021-04-15 12:00:00 +02:00
|
|
|
$this->table,
|
|
|
|
$values,
|
|
|
|
['id_evento' => $values['id_evento']]
|
2020-10-06 15:48:12 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|