Cacti (home)ForumsDocumentation
Cacti: offical forums and support
It is currently Thu Mar 23, 2017 7:19 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: spine/cacti patch SNMP to allow hostname like user@host:port
PostPosted: Mon Feb 27, 2012 9:02 am 
Offline

Joined: Sat Jul 02, 2005 7:12 pm
Posts: 19
'lo all,

I've been working in an environment where there's lots of different user/host/ports configs, resulting in a whole set of "devices" to monitor one and the same host ...
changed already some polling scripts to allow for "user@host:port" host names in Cacti.

These patches allow for SNMP polls to host names in the form of "user@host:port", "user@host", "host:port" or just plain "host" of course

Here's the patches for "cacti-spine-0.8.7i", but it'll probably work fine with other versions too...

<ping.c>
Code:
[eso@el0758 cacti-spine-0.8.7i]$ diff -u ../diff/cacti-spine-0.8.7i/ping.c ping.c
--- ../diff/cacti-spine-0.8.7i/ping.c 2011-12-05 18:39:43.000000000 +0100
+++ ping.c 2012-02-29 10:02:11.000000000 +0100
@@ -937,7 +937,9 @@
           strcpy(cleaned_hostname, hostname);
        }

-       return(cleaned_hostname);
+    /* remove @ and : characters ... */
+       char *sane_hostname = sane(cleaned_hostname);
+       return(sane_hostname);
}

/*! \fn unsigned short int get_checksum(void* buf, int len)


<ping.h>
Code:
[eso@el0758 cacti-spine-0.8.7i]$ diff -u ../diff/cacti-spine-0.8.7i/ping.h ping.h
--- ../diff/cacti-spine-0.8.7i/ping.h 2011-01-27 05:02:45.000000000 +0100
+++ ping.h 2012-02-29 09:51:25.000000000 +0100
@@ -140,6 +140,7 @@
extern int ping_udp(host_t *host, ping_t *ping);
extern int ping_tcp(host_t *host, ping_t *ping);
extern char *remove_tcp_udp_from_hostname(char *hostname);
+extern char *sane(char *hostname);
extern void update_host_status(int status, host_t *host, ping_t *ping, int availability_method);
extern int init_sockaddr(struct sockaddr_in *name, const char *hostname, unsigned short int port);
extern unsigned short int get_checksum(void* buf, int len);


<poller.c>
Code:
[eso@el0758 cacti-spine-0.8.7i]$ diff -u ../diff/cacti-spine-0.8.7i/poller.c poller.c
--- ../diff/cacti-spine-0.8.7i/poller.c 2011-09-25 23:12:21.000000000 +0200
+++ poller.c 2012-02-29 12:33:41.000000000 +0100
@@ -17,7 +17,7 @@
  | License along with this library; if not, write to the Free Software     |
  | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA           |
  | 02110-1301, USA                                                         |
- |                                                                         |
+|                                                                         |
  +-------------------------------------------------------------------------+
  | spine: a backend data gatherer for cacti                                |
  +-------------------------------------------------------------------------+
@@ -34,6 +34,44 @@
#include "common.h"
#include "spine.h"

+/* gert sanitize */
+char *sane(char *hostname) {
+
+  int null ;
+  char *hostptr ;
+  if (!(hostptr = (char *) malloc(strlen(hostname)+1))) {
+         die("ERROR: Fatal malloc error: poller.c sane()") ;
+  }
+  /* Search for [@|:] in input string */
+  char *atp = strrchr(hostname, '@' ) ;
+  char *cop = strrchr(hostname, ':' ) ;
+  /* If @&&: found: */
+  if((atp)&&(cop)) {
+       null =  strlen(atp)-strlen(cop) ;
+       memcpy(hostptr,atp+1,null) ;
+  }
+  /* if only @ */
+  if((atp)&&(!cop)) {
+    null = strlen(atp) ;
+    memcpy(hostptr,atp+1,null) ;
+  }
+  /* if only : */
+  if((!atp)&&(cop)) {
+    null = strlen(hostname)-strlen(cop)+1 ;
+    memcpy(hostptr,hostname,null) ;
+  }
+  /* if !@: */
+  if((!atp)&&(!cop)) {
+       null = strlen(hostname)+1 ;
+       memcpy(hostptr,hostname,null);
+  }
+
+  /* NUL termination */
+  hostptr[null-1]='\0' ;
+  return(hostptr);
+  free(hostptr);
+}
+
/*! \fn void *child(void *arg)
  *  \brief function is called via the fork command and initiates a poll of a host
  *  \param arg a pointer to an integer point to the host_id to be polled
@@ -463,8 +501,9 @@
             if (((host->snmp_version >= 1) && (host->snmp_version <= 2) &&
               (strlen(host->snmp_community) > 0)) ||
               (host->snmp_version == 3)) {
+              /* - gert - */
               host->snmp_session = snmp_host_init(host->id,
-               host->hostname,
+               sane(host->hostname),
                host->snmp_version,
                host->snmp_community,
                host->snmp_username,
@@ -875,7 +914,7 @@
                                        STRNCOPY(last_snmp_priv_protocol,   poller_items[i].snmp_priv_protocol);
                                        STRNCOPY(last_snmp_context,         poller_items[i].snmp_context);

-                                       host->snmp_session = snmp_host_init(host->id, poller_items[i].hostname,
+                                       host->snmp_session = snmp_host_init(host->id, sane(poller_items[i].hostname),
                                                poller_items[i].snmp_version, poller_items[i].snmp_community,
                                                poller_items[i].snmp_username, poller_items[i].snmp_password,
                                                poller_items[i].snmp_auth_protocol, poller_items[i].snmp_priv_passphrase,


And I forgot the patch for lib/snmp.php, to allow the cacti interface to call the correct hostname too ...
Code:
[eso@el0758 lib]$ diff -u cacti-0.8.7i-PIA-3.1/lib/snmp.php snmp.php
--- cacti-0.8.7i-PIA-3.1/lib/snmp.php   2011-12-12 02:56:06.000000000 +0100
+++ snmp.php    2012-03-01 14:00:57.000000000 +0100
@@ -27,9 +27,19 @@
define("SNMP_METHOD_PHP", 1);
define("SNMP_METHOD_BINARY", 2);

+function sane($hostname) {
+   $host_str_at = preg_replace('/.*@/', '', $hostname) ;
+   $host_str_sane = preg_replace('/:(.*)/', '', $host_str_at) ;
+   return $host_str_sane ;
+}
+
function cacti_snmp_get($hostname, $community, $oid, $version, $username, $password, $auth_proto, $priv_pass, $priv_proto, $context, $port = 161, $timeout = 500, $retries = 0, $environ = SNMP_POLLER) {
        global $config;

+       /* - gert - remove @&&: from hostnames */
+       $hostname = sane($hostname) ;
+       //cacti_log("SANETIZE: '$hostname'");
+
        /* determine default retries */
        if (($retries == 0) || (!is_numeric($retries))) {
                $retries = read_config_option("snmp_retries");
@@ -146,6 +156,10 @@
function cacti_snmp_getnext($hostname, $community, $oid, $version, $username, $password, $auth_proto, $priv_pass, $priv_proto, $context, $port = 161, $timeout = 500, $retries = 0, $environ = SNMP_POLLER) {
        global $config;

+        /* - gert - remove @&&: from hostnames */
+        $hostname = sane($hostname) ;
+        //cacti_log("SANETIZE: '$hostname'");
+
        /* determine default retries */
        if (($retries == 0) || (!is_numeric($retries))) {
                $retries = read_config_option("snmp_retries");
@@ -257,6 +271,10 @@
function cacti_snmp_walk($hostname, $community, $oid, $version, $username, $password, $auth_proto, $priv_pass, $priv_proto, $context, $port = 161, $timeout = 500, $retries = 0, $max_oids = 10, $environ = SNMP_POLLER) {
        global $config, $banned_snmp_strings;

+        /* - gert - remove @&&: from hostnames */
+        $hostname = sane($hostname) ;
+        //cacti_log("SANETIZE: '$hostname'");
+
        $snmp_oid_included = true;
        $snmp_auth             = '';
        $snmp_array        = array();


grtz,
gert


Top
 Profile  
 
 Post subject: Re: spine/cacti patch SNMP to allow hostname like user@host:
PostPosted: Tue Apr 03, 2012 9:41 pm 
Offline
Developer/Forum Admin
User avatar

Joined: Mon Nov 17, 2003 6:35 pm
Posts: 5938
Location: Michigan, USA
Please submit a bug in the bug system: http://www.cacti.net/bugs.php

_________________
Tony Roman
Experience is what causes a person to make new mistakes instead of old ones.
There are only 3 way to complete a project: Good, Fast or Cheap, pick two.
With age comes wisdom, what you choose to do with it determines whether or not you are wise.


Top
 Profile  
 
 Post subject: Re: spine/cacti patch SNMP to allow hostname like user@host:
PostPosted: Tue Apr 10, 2012 3:31 am 
Offline
Developer
User avatar

Joined: Thu Dec 02, 2004 2:46 am
Posts: 22461
Location: Muenster, Germany
I only find, that "host" is pulled out of "user@host:port". Is "user" and "port" already handled correctly?
R.

_________________
Official Cacti Documentation
Official Debugging Help
Central Plugin Repository
Central Templates Repository


Top
 Profile  
 
 Post subject: Re: spine/cacti patch SNMP to allow hostname like user@host:
PostPosted: Mon Apr 16, 2012 3:14 am 
Offline

Joined: Sat Jul 02, 2005 7:12 pm
Posts: 19
Hy,

"host" is the only part that should be pulled out when spine snmp_polls the device ...

http://bugs.cacti.net/view.php?id=2220

The "user@host:port" hostname definitions, extract "user" and "port" when polling other services via script_commands. I find, it makes the "hostname" definition of a device more dynamic and less dependant on accompanying config files or hard-coded stuff inside poll scripts/templates.

grtz,
gert

PS : i've also patched Percona mysql/apache/jboss check scripts to use "user" and "port" for polling (haven't submitted patch yet though)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  

Protected by Anti-Spam ACP Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group