Skip to content

Commit

Permalink
hostgroups + schema update
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickpr committed Jan 13, 2019
1 parent 4bbe239 commit 57ce7d3
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 32 deletions.
3 changes: 3 additions & 0 deletions SQL/schema_v1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ CREATE TABLE `#PREFIX#received` (
`trap_name` varchar(256) DEFAULT NULL,
`source_name` varchar(256) DEFAULT NULL,
`trap_name_mib` varchar(100) DEFAULT NULL,
`process_time` float DEFAULT '0',
`status_detail` varchar(256) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

Expand Down Expand Up @@ -50,5 +52,6 @@ CREATE TABLE `#PREFIX#rules` (
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
`modifier` varchar(100) DEFAULT NULL,
`num_match` int(16) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
25 changes: 13 additions & 12 deletions application/controllers/HelperController.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,6 @@ public function getmiblistAction()

public function translateoidAction()
{
// TODO : get binary & dirs from config / database
$snmptranslate=$this->Config()->get('config', 'snmptranslate');
$snmptranslate_dirs=$this->Config()->get('config', 'snmptranslate_dirs');

$postData=$this->getRequest()->getPost();
if (isset($postData['oid']))
{
Expand All @@ -238,16 +234,21 @@ public function translateoidAction()
}

// Try to get oid name from snmptranslate
$translate=exec($snmptranslate . ' -m ALL -M '.$snmptranslate_dirs.
' '.$oid,$translate_output);
$ret_code=preg_match('/(.*)::(.*)/',$translate,$matches);
if ($ret_code==0 || $ret_code==FALSE) {
$this->_helper->json(array('status'=>'Not found'));;
} else {
if (($object=$this->getMIB()->translateOID($oid)) == null)
{
$this->_helper->json(array('status'=>'Not found'));
}
else
{
$this->_helper->json(
array('status'=>'OK','mib' => $matches[1], 'name' => $matches[2])
array('status'=>'OK',
'mib' => $object['mib'],
'name' => $object['name'],
'type' => $object['type']
)
);
}
}

}

/** Save or execute database purge of <n> days
Expand Down
15 changes: 11 additions & 4 deletions application/views/scripts/handler/add.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ function deleteMain()
});
}

function submitMain()
function submitMain()
{
// Check everything
// Hostname/hostgroup
Expand All @@ -354,10 +354,17 @@ function deleteMain()
servicename=$("#id_service option:selected").text();
serviceid=$("#id_service").val();
}

// Get OID
var oidRegexp = /^[\.\d]+$/;
if ( ! oidRegexp.test($("#id_mainoid").val()) ) { displayWarning("Invalid oid");return; }
var getOID=$("#id_mainoid").val();
if ( ! oidRegexp.test(getOID) ) { displayWarning("Invalid oid");return; }
// Check if first car is a '.'
if (getOID.charAt(0) != '.')
{
getOID = '.'+getOID;
}

// Get revert time
var timeRegexp = /^[\d]*$/;
var revertOK=$("#id_revertOK").val()
if (revertOK=='') revertOK=0;
Expand Down Expand Up @@ -387,7 +394,7 @@ function deleteMain()
"hostname" : hostname,
"serviceid" : serviceid,
"serviceName" : servicename,
"oid" : $("#id_mainoid").val(),
"oid" : getOID,
"revertOK" : revertOK,
"display" : newDisplay,
"rule" : newRule,
Expand Down
88 changes: 82 additions & 6 deletions bin/trap_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ class Trap
protected $icinga2cmd='/var/run/icinga2/cmd/icinga2.cmd';
protected $db_prefix='traps_';

// Options from config database
// None for now

// Logs
//**** Options from config database
// Logs
protected $debug_level=2; // 0=No output 1=critical 2=warning 3=trace 4=ALL
protected $alert_output='syslog'; // alert type : file, syslog, display
protected $debug_file="/tmp/trapdebug.txt";
protected $syslog_logfile=null; // TODO check if useful
//**** End options from database

//protected $debug_file="php://stdout";
// Databases
Expand All @@ -31,6 +30,8 @@ class Trap
protected $receivingHost;
public $trap_data=array(); //< Main trap data (oid, source...)
public $trap_data_ext=array(); //< Additional trap data objects (oid/value).
public $trap_id=null; //< trap_id after sql insert
public $trap_action=null; //< trap action for final write

function __construct($etc_dir='/etc/icingaweb2')
{
Expand Down Expand Up @@ -450,6 +451,7 @@ public function writeTrapToDB()

// TODO check value of $inserted_id
$inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()'];
$this->trap_id=$inserted_id;
$this->trapLog('id found: '.$inserted_id,3,'');

// Fill trap extended data table
Expand Down Expand Up @@ -488,12 +490,67 @@ public function writeTrapToDB()
protected function getRules($ip,$oid)
{
$db_conn=$this->db_connect_trap();
$sql='SELECT * from '.$this->db_prefix.'rules WHERE ( ( ip4=\''.$ip.'\' OR ip6=\''.$ip.'\' ) AND trap_oid=\''.$oid.'\' )';
// fetch rules based on IP in rule and OID
$sql='SELECT * from '.$this->db_prefix.'rules WHERE trap_oid=\''.$oid.'\' ';
$this->trapLog('SQL query : '.$sql,4,'');
if (($ret_code=$db_conn->query($sql)) == FALSE) {
$this->trapLog('No result in query : ' . $sql,2,'');
}
return $ret_code->fetchAll();
$rules_all=$ret_code->fetchAll();
//echo "rule all :\n";print_r($rules_all);echo "\n";
$rules_ret=array();
$rule_ret_key=0;
foreach ($rules_all as $key => $rule)
{
if ($rule['ip4']==$ip || $rule['ip6']==$ip)
{
$rules_ret[$rule_ret_key]=$rules_all[$key];
$rule_ret_key++;
continue;
}
if (isset($rule['host_group_name']) && $rule['host_group_name']!=null)
{ // get ips of group members by oid
$db_conn2=$this->db_connect_ido();
$sql="SELECT m.host_object_id, a.address as ip4, a.address6 as ip6, b.name1 as host_name
FROM icinga_objects as o
LEFT JOIN icinga_hostgroups as h ON o.object_id=h.hostgroup_object_id
LEFT JOIN icinga_hostgroup_members as m ON h.hostgroup_id=m.hostgroup_id
LEFT JOIN icinga_hosts as a ON a.host_object_id = m.host_object_id
LEFT JOIN icinga_objects as b ON b.object_id = a.host_object_id
WHERE o.name1='".$rule['host_group_name']."';";
if (($ret_code2=$db_conn2->query($sql)) == FALSE) {
$this->trapLog('No result in query : ' . $sql,2,'');
continue;
}
$grouphosts=$ret_code2->fetchAll();
//echo "rule grp :\n";print_r($grouphosts);echo "\n";
foreach ( $grouphosts as $gkey=>$host)
{
//echo $host['ip4']."\n";
if ($host['ip4']==$ip || $host['ip6']==$ip)
{
//echo "Rule added \n";
$rules_ret[$rule_ret_key]=$rules_all[$key];
$rules_ret[$rule_ret_key]['host_name']=$host['host_name'];
$rule_ret_key++;
}
}
}
}
//echo "rule rest :\n";print_r($rules_ret);echo "\n";exit(0);
return $rules_ret;
}

/** Add rule match to rule
* @param rule id
*/
protected function add_rule_match($id, $set)
{
$db_conn=$this->db_connect_trap();
$sql="UPDATE ".$this->db_prefix."rules SET num_match = '".$set."' WHERE (id = '".$id."');";
if (($ret_code=$db_conn->query($sql)) == FALSE) {
$this->trapLog('Error in update query : ' . $sql,2,'');
}
}

/** Send SERVICE_CHECK_RESULT
Expand Down Expand Up @@ -754,6 +811,7 @@ protected function eval_rule($rule)
public function applyRules()
{
$rules = $this->getRules($this->trap_data['source_ip'],$this->trap_data['trap_oid']);

if ($rules==FALSE || count($rules)==0)
{
$this->trapLog('No rules found for this trap',3,'');
Expand Down Expand Up @@ -782,6 +840,8 @@ public function applyRules()
if ($action != -1)
{
$this->serviceCheckResult($host_name,$service_name,$action,$display);
$this->add_rule_match($rule['id'],$rule['num_match']+1);
$this->trap_action='Status '.$action.' to '.$host_name.'/'.$service_name;
}
}
else
Expand All @@ -793,6 +853,8 @@ public function applyRules()
if ($action != -1)
{
$this->serviceCheckResult($host_name,$service_name,$action,$display);
$this->add_rule_match($rule['id'],$rule['num_match']+1);
$this->trap_action='Status '.$action.' to '.$host_name.'/'.$service_name;
}
}
// Put name in source_name
Expand All @@ -818,6 +880,20 @@ public function applyRules()
$this->trap_data['status']='done';
}

/** Add Time a action to rule
* @param rule id
*/
public function add_rule_final($time)
{
$db_conn=$this->db_connect_trap();
$sql="UPDATE ".$this->db_prefix."received SET process_time = '".$time."' , status_detail='".$this->trap_action."' WHERE (id = '".$this->trap_id."');";
if (($ret_code=$db_conn->query($sql)) == FALSE) {
$this->trapLog('Error in update query : ' . $sql,2,'');
}
}

/*********** UTILITIES *********************/

/** Create database schema
* @param $schema_file File to read schema from
* @param $table_prefix to replace #PREFIX# in schema file by this
Expand Down
12 changes: 10 additions & 2 deletions bin/trap_in.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

// start
$time1 = microtime(true);

require_once ('trap_class.php');


Expand All @@ -13,24 +16,29 @@

// Set by DB
//$Trap->setLogging($debug_level,'syslog');
//$Trap->setLogging($debug_level,'file');
//$Trap->setLogging($debug_level,'display');

// TODO : tranfer this to reset_trap script
$Trap->eraseOldTraps();

try
{
$data=$Trap->read_trap('php://stdin');


$Trap->applyRules();

$Trap->writeTrapToDB();

}
catch (Exception $e)
{
$Trap->trapLog("Exception trapped : ". $e->getMessage(),2,0);
}

//end
$Trap->add_rule_final(microtime(true) - $time1);

exit(0);

/*
Expand Down
29 changes: 29 additions & 0 deletions docs/40-optimize.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Optimize trap reception
===============


snmptrapd
---------------

In : /usr/lib/systemd/system/snmptrapd.service

Basic :

Environment=OPTIONS="-Lsd"

Better :

Environment=OPTIONS="-Lsd -n -t -On"

-n : no hostame resolution

-t : disable trap to syslog

-p : file for process id (useful?)

-On : no oid translate

Temp snmpd file in memory :

tmpfs /var/run/snmpd tmpfs defaults,size=128m 0 0

25 changes: 17 additions & 8 deletions library/Trapdirector/Config/TrapModuleConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public function getTrapListDisplayColumns()
'source_ip' => 'CASE WHEN t.source_name IS NULL THEN t.source_ip ELSE t.source_name END as source_ip',
'trap_oid' => "CASE WHEN t.trap_name IS NULL OR t.trap_name = '' THEN t.trap_oid ELSE t.trap_name END",
'status' => 't.status',
'status_detail' => 't.status_detail',
'process_time' => 't.process_time',
'id' => 't.id',
//'destination_port' => 't.destination_port',
);
Expand All @@ -88,6 +90,8 @@ public function getTrapListTitles()
'source_ip' => 'Source IP/name',
'trap_oid' => 'Trap OID',
'status' => 'Status',
'status_detail' => 'Status detail',
'process_time' => 'Processing time',
//'destination_port' => 'Destination Port',
//'id' => 'Id',
);
Expand All @@ -106,6 +110,7 @@ public function getHandlerListDisplayColumns()
'action_match' => 'r.action_match',
'action_nomatch'=> 'r.action_nomatch',
'service_name' => 'r.service_name',
'num_match' => 'r.num_match',
'id' => 'r.id'
);
}
Expand All @@ -119,7 +124,8 @@ public function getHandlerListTitles()
'rule' => 'Rule',
'action_match' => 'On rule match',
'action_nomatch'=> 'On rule dont match',
'service_name' => 'Service Name',
'service_name' => 'Service Name',
'num_match' => 'Has matched'
//'id' => 'Id',
);
}
Expand Down Expand Up @@ -151,15 +157,18 @@ public function urlPath() { return 'trapdirector'; }
public function trapDetailQuery()
{
return array(
'timestamp' => array('Date','UNIX_TIMESTAMP(t.date_received)'),
'source_ip' => array('Source IP','t.source_ip'),
'timestamp' => array('Date','UNIX_TIMESTAMP(t.date_received)'),
'source_ip' => array('Source IP','t.source_ip'),
'source_name' => array('Source name','t.source_name'),
'source_port' => array('Source port','t.source_port'),
'destination_ip' => array('Destination IP','t.destination_ip'),
'destination_port' => array('Destination port','t.destination_port'),
'trap_oid' => array('Numeric OID','t.trap_oid'),
'trap_name' => array('Trap name','t.trap_name'),
'destination_ip' => array('Destination IP','t.destination_ip'),
'destination_port' => array('Destination port','t.destination_port'),
'trap_oid' => array('Numeric OID','t.trap_oid'),
'trap_name' => array('Trap name','t.trap_name'),
'trap_name_mib' => array('Trap MIB','t.trap_name_mib'),
'status' => array('Processing status','t.status'),
'status' => array('Processing status','t.status'),
'status_detail' => array('Status details','t.status_detail'),
'process_time' => array('Trap processing time','t.process_time'),
);
}
// Trap detail : additional data (<key> => <title> <sql select>)
Expand Down

0 comments on commit 57ce7d3

Please sign in to comment.