diff --git a/cactid.c b/cactid.c index 12bf5e3..10c4e81 100644 --- a/cactid.c +++ b/cactid.c @@ -202,7 +202,7 @@ int main(int argc, char *argv[]) { pthread_attr_init(&attr); // pthread_mutexattr_settype(&mutexattr); - pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_ERRORCHECK); +// pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_ERRORCHECK); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); init_mutexes(); diff --git a/cactid.h b/cactid.h index 6dbb3fc..0ece1c5 100644 --- a/cactid.h +++ b/cactid.h @@ -70,6 +70,7 @@ #define LOCK_RRDTOOL 3 #define LOCK_PIPE 4 #define LOCK_SYSLOG 5 +#define LOCK_PHP 6 #define LOCK_SNMP_O 0 #define LOCK_THREAD_O 1 @@ -77,6 +78,7 @@ #define LOCK_RRDTOOL_O 3 #define LOCK_PIPE_O 4 #define LOCK_SYSLOG_O 5 +#define LOCK_PHP_O 6 #define POLLER_ACTION_SNMP 0 #define POLLER_ACTION_SCRIPT 1 diff --git a/locks.c b/locks.c index c140f61..25d8cef 100644 --- a/locks.c +++ b/locks.c @@ -34,6 +34,7 @@ pthread_mutex_t mysql_lock; pthread_mutex_t rrdtool_lock; pthread_mutex_t pipe_lock; pthread_mutex_t syslog_lock; +pthread_mutex_t php_lock; pthread_once_t snmp_lock_o = PTHREAD_ONCE_INIT; pthread_once_t threads_lock_o = PTHREAD_ONCE_INIT; @@ -41,6 +42,7 @@ pthread_once_t mysql_lock_o = PTHREAD_ONCE_INIT; pthread_once_t rrdtool_lock_o = PTHREAD_ONCE_INIT; pthread_once_t pipe_lock_o = PTHREAD_ONCE_INIT; pthread_once_t syslog_lock_o = PTHREAD_ONCE_INIT; +pthread_once_t php_lock_o = PTHREAD_ONCE_INIT; static void init_snmp_lock(void) { pthread_mutex_init(&snmp_lock, PTHREAD_MUTEXATTR_DEFAULT); @@ -66,6 +68,10 @@ static void init_syslog_lock(void) { pthread_mutex_init(&syslog_lock, PTHREAD_MUTEXATTR_DEFAULT); } +static void init_php_lock(void) { + pthread_mutex_init(&php_lock, PTHREAD_MUTEXATTR_DEFAULT); +} + void init_mutexes() { pthread_once((pthread_once_t*) get_attr(LOCK_SNMP_O), init_snmp_lock); pthread_once((pthread_once_t*) get_attr(LOCK_THREAD_O), init_thread_lock); @@ -73,6 +79,7 @@ void init_mutexes() { pthread_once((pthread_once_t*) get_attr(LOCK_RRDTOOL_O), init_rrdtool_lock); pthread_once((pthread_once_t*) get_attr(LOCK_PIPE_O), init_pipe_lock); pthread_once((pthread_once_t*) get_attr(LOCK_SYSLOG_O), init_syslog_lock); + pthread_once((pthread_once_t*) get_attr(LOCK_PHP_O), init_php_lock); } pthread_mutex_t* get_lock(int lock) { @@ -97,6 +104,9 @@ pthread_mutex_t* get_lock(int lock) { case LOCK_SYSLOG: ret_val = &syslog_lock; break; + case LOCK_PHP: + ret_val = &php_lock; + break; } return ret_val; @@ -124,6 +134,9 @@ pthread_once_t* get_attr(int locko) { case LOCK_SYSLOG_O: ret_val = &syslog_lock_o; break; + case LOCK_PHP_O: + ret_val = &php_lock_o; + break; } return ret_val; diff --git a/locks.h b/locks.h index b5bd7a4..736f779 100644 --- a/locks.h +++ b/locks.h @@ -29,6 +29,7 @@ static void init_mysql_lock(void); static void init_rrdtool_lock(void); static void init_pipe_lock(void); static void init_syslog_lock(void); +static void init_php_lock(void); void init_mutexes(void); void thread_mutex_lock(int); void thread_mutex_unlock(int); diff --git a/php.c b/php.c index 50a54a1..cd69935 100644 --- a/php.c +++ b/php.c @@ -46,18 +46,30 @@ extern char **environ; /******************************************************************************/ char *php_cmd(char *php_command) { char *result_string; + char *spaceloc; char command[BUFSIZE+5]; /* pad command with CR-LF */ sprintf(command,php_command,strlen(php_command)); strcat(command,"\r\n"); + thread_mutex_lock(LOCK_PHP); /* send command to the script server */ write(php_pipes.php_write_fd, command, strlen(command)); /* read the result from the php_command */ result_string = php_readpipe(); + /* Clean garbage from string. Don't know why it's there... */ + spaceloc = strchr(result_string, ' '); + if (spaceloc != 0) { + *spaceloc = '\0'; + spaceloc = strchr(result_string, ' '); + if (spaceloc != 0) + *spaceloc = '\0'; + } + thread_mutex_unlock(LOCK_PHP); + return result_string; } @@ -65,8 +77,8 @@ char *php_cmd(char *php_command) { /* php_readpipe - Read a line from the PHP Script Server */ /******************************************************************************/ char *php_readpipe() { - char result[BUFSIZE]; char *result_string = (char *) malloc(BUFSIZE); + char result[BUFSIZE] = ""; fd_set fds; int rescode, numfds; struct timeval timeout; @@ -82,7 +94,7 @@ char *php_readpipe() { numfds = php_pipes.php_write_fd + 1; /* Establish Timeout of 1 Second to Have PHP Script Server Respond */ - timeout.tv_sec = 1; + timeout.tv_sec = 3; timeout.tv_usec = 0; /* Wait for A Response on The Pipes */ @@ -92,9 +104,13 @@ char *php_readpipe() { /* Should only be the READ Pipe */ if (FD_ISSET(php_pipes.php_read_fd, &fds)) { rescode = read(php_pipes.php_read_fd, result_string, BUFSIZE); + if (rescode > 0) + snprintf(result_string, rescode, "%s", result_string); + else + snprintf(result_string, 2, "%s", "U"); } else { cacti_log("ERROR: The PHP Script Server Did not Respond in Time\n","e"); - snprintf(result_string, BUFSIZE, "%s", "U"); + snprintf(result_string, 2, "%s", "U"); } return result_string; @@ -109,7 +125,7 @@ int php_init() { char logmessage[255]; int i = 0; int pid; - char *argv[3]; + char *argv[4]; int check; int cancel_state; char *result_string; @@ -136,7 +152,8 @@ int php_init() { /* establish arguments for script server execution */ argv[0] = set.phppath; argv[1] = set.path_php_server; - argv[2] = NULL; + argv[2] = "cactid"; + argv[3] = NULL; /* fork a child process */ if (set.verbose >= DEBUG) { @@ -197,15 +214,9 @@ int php_init() { result_string = php_readpipe(); free(result_string); - if ((set.verbose == DEBUG) && (strstr(result_string, "Started"))) + if ((set.verbose == DEBUG) && (strstr(result_string, "Started"))) cacti_log("CACTID: Confirmed PHP Script Server Running\n","e"); - /* Make a sample call to the pipe to see if everything is running */ - result_string = php_cmd("C:/wwwroot/cacti/scripts/ss_query_host_cpu.php ss_query_host_cpu 192.168.0.2 public 1 get usage 0"); -// printf("The result was ->%s\n",result_string); - -// free(result_string); - return 1; } diff --git a/poller.c b/poller.c index 4fca299..8a3ed95 100644 --- a/poller.c +++ b/poller.c @@ -213,7 +213,7 @@ void poll_host(int host_id) { } if (set.verbose >= HIGH) { - printf("SNMPGET COMPLETE: Host [%i]: v%i: %s, dsname: %s, oid: %s, value: %s\n", host_id, host->snmp_version, host->hostname, entry->rrd_name, entry->arg1, entry->result); + printf("SNMPGET: Host [%i]: v%i: %s, dsname: %s, oid: %s, value: %s\n", host_id, host->snmp_version, host->hostname, entry->rrd_name, entry->arg1, entry->result); } break; @@ -223,18 +223,17 @@ void poll_host(int host_id) { free(poll_result); if (set.verbose >= HIGH) { - printf("POLL COMPLETE: Command [%i]: %s, output: %s\n", host_id, entry->arg1, entry->result); + printf("SCRIPT: CMD [%i]: %s, output: %s\n", host_id, entry->arg1, entry->result); } break; case POLLER_ACTION_PHP_SCRIPT_SERVER: /* execute script server */ -// poll_result = php_cmd(entry->arg1); -//printf("The Poll Result was ->%s\n", poll_result); -// snprintf(entry->result, sizeof(entry->result), "%s", poll_result); -// free(poll_result); + poll_result = php_cmd(entry->arg1); + snprintf(entry->result, sizeof(entry->result), "%s", poll_result); + free(poll_result); if (set.verbose >= HIGH) { - printf("PHP SERVER COMPLETE: Command [%i]: %s, output: %s\n", host_id, entry->arg1, entry->result); + printf("PHPSERVER: CMD [%i]: %s, output: %s\n", host_id, entry->arg1, entry->result); } break;