| 1 |  | /* | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 2 |  | * i-scream central monitoring system | 
 
 
 
 
 
 
 
 | 3 | + | * http://www.i-scream.org.uk | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 4 |  | * Copyright (C) 2000-2002 i-scream | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 5 |  | * | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 6 |  | * This program is free software; you can redistribute it and/or | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 18 |  | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 19 |  | */ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 20 |  |  | 
 
 
 
 
 
 
 
 | 21 | + | #ifdef HAVE_CONFIG_H | 
 
 
 
 
 
 
 
 | 22 | + | #include "config.h" | 
 
 
 
 
 
 
 
 | 23 | + | #endif | 
 
 
 
 
 
 
 
 | 24 | + |  | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 25 |  | #include <stdio.h> | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 26 |  | #include <stdlib.h> | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 27 |  | #include <unistd.h> | 
 
 
 
 
 
 
 
 
 | 23 | – | #include <syslog.h> | 
 
 
 
 
 
 
 
 
 | 24 | – | #include <netinet/in.h> | 
 
 
 
 
 
 
 
 
 | 25 | – | #include "ukcprog.h" | 
 
 
 
 
 
 
 
 
 | 26 | – | #include <netdb.h> | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 28 |  | #include <string.h> | 
 
 
 
 
 
 
 
 
 
 
 | 29 | < | #include "statgrab.h" | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 29 | < | #include <time.h> | 
 
 
 
 
 
 
 
 
 | 29 | > | #include <sys/types.h> | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 30 |  | #include <sys/socket.h> | 
 
 
 
 
 
 
 
 | 31 | + | #include <stdarg.h> | 
 
 
 
 
 
 
 
 | 32 | + | #include <errno.h> | 
 
 
 
 
 
 
 
 | 33 | + | #include <netdb.h> | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 34 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 35 | < | #define RECONFIGURE_RETURN_CODE 2 | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 36 | < | #define UDP_MAX_PACKET_SIZE 8192 | 
 
 
 
 
 
 
 
 
 | 35 | > | #include "ukcprog.h" | 
 
 
 
 
 | 36 | > | #include "statgrab.h" | 
 
 
 
 
 
 
 
 
 
 
 | 37 |  |  | 
 
 
 
 
 
 
 
 | 38 | + | #define LOG_CRIT 0 | 
 
 
 
 
 
 
 
 | 39 | + | #define LOG_ERR 1 | 
 
 
 
 
 
 
 
 | 40 | + | #define LOG_INFO 2 | 
 
 
 
 
 
 
 
 | 41 | + | #define LOG_DEBUG 3 | 
 
 
 
 
 
 
 
 | 42 | + |  | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 43 |  | typedef struct{ | 
 
 
 
 
 
 
 
 
 
 
 | 44 | < | int fm_port; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 45 | < | char *fm_host; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 46 | < |  | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 47 | < | char *my_fqdn; | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 44 | > | int filtermanager_port; | 
 
 
 
 
 | 45 | > | char *filtermanager_host; | 
 
 
 
 
 | 46 | > |  | 
 
 
 
 
 | 47 | > | char *host_ip; | 
 
 
 
 
 | 48 | > | char *host_fqdn; | 
 
 
 
 
 | 49 | > |  | 
 
 
 
 
 
 
 
 
 
 
 | 50 |  | char *server_fqdn; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 51 |  | int server_udp_port; | 
 
 
 
 
 
 
 
 
 
 
 | 52 | < | int server_tcp_port; | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 52 | > |  | 
 
 
 
 
 | 53 | > | /* Weird stuff iscream wants sent to it */ | 
 
 
 
 
 
 
 
 
 
 
 | 54 |  | char *last_modified; | 
 
 
 
 
 
 
 
 
 
 
 | 55 | < | char *files_list; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 56 | < | char *key; | 
 
 
 
 
 
 
 
 
 | 55 | > | char *file_list; | 
 
 
 
 
 | 56 | > |  | 
 
 
 
 
 
 
 
 
 
 
 | 57 |  | int udp_update_time; | 
 
 
 
 
 
 
 
 
 
 
 | 58 | < | int tcp_update_time; | 
 
 
 
 
 
 
 
 
 | 58 | > | //      int config_ttl; | 
 
 
 
 
 
 
 
 
 
 
 | 59 |  |  | 
 
 
 
 
 
 
 
 | 60 | + | time_t config_ttl; | 
 
 
 
 
 
 
 
 | 61 | + |  | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 62 |  | }ihost_state_t; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 63 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 64 | < | char* sock_comm(FILE *f_r, FILE *f_w, char *sendString){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 65 | < | char *reply; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 66 | < | fprintf(f_w, "%s\n", sendString); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 54 | < | fflush(f_w); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 55 | < | reply=fpgetline(f_r); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 56 | < | /* Returns pointer to static buffer */ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 57 | < | return reply; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 58 | < | } | 
 
 
 
 
 
 
 
 
 | 64 | > | typedef struct{ | 
 
 
 
 
 | 65 | > | int verbose; | 
 
 
 
 
 | 66 | > | int daemon; | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 67 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 68 | < | int ihost_configure(ihost_state_t *ihost_state){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 68 | > | FILE *log; | 
 
 
 
 
 | 69 | > | }ihost_config_t; | 
 
 
 
 
 | 70 | > |  | 
 
 
 
 
 | 71 | > | typedef struct{ | 
 
 
 
 
 
 
 
 
 
 
 | 72 |  | struct sockaddr_in addr; | 
 
 
 
 
 
 
 
 
 
 
 | 73 | < | struct in_addr haddr; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 74 | < | int sd; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 64 | < | FILE *fm_fd_r, *fm_fd_w; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 65 | < | char *reply; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 66 | < | char *reply_ptr; | 
 
 
 
 
 
 
 
 
 | 73 | > | int sock; | 
 
 
 
 
 | 74 | > | }udp_sockinfo_t; | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 75 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 76 | < | /* Check to see if anything needs to be free'd */ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 69 | < | if (ihost_state->my_fqdn!=NULL) free(ihost_state->my_fqdn); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 70 | < | if (ihost_state->server_fqdn!=NULL) free(ihost_state->server_fqdn); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 71 | < | if (ihost_state->last_modified!=NULL) free(ihost_state->last_modified); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 72 | < | if (ihost_state->files_list!=NULL) free(ihost_state->files_list); | 
 
 
 
 
 
 
 
 
 | 76 | > | ihost_config_t ihost_config; | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 77 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 78 | < | if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) { | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 79 | < | errf("Can't create AF_INET socket (%m)"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 80 | < | return -1; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 81 | < | } | 
 
 
 
 
 
 
 
 
 | 78 | > | void log_msg(int level, char *format, ...){ | 
 
 
 
 
 | 79 | > | extern int errno; | 
 
 
 
 
 | 80 | > | int cur_errno; | 
 
 
 
 
 | 81 | > | va_list ap; | 
 
 
 
 
 
 
 
 
 
 
 | 82 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 83 | < | if (get_host_addr(ihost_state->fm_host, &haddr) != 0){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 84 | < | errf("Failed to resolve address %s (%m)", ihost_state->fm_host); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 85 | < | return -1; | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 83 | > | cur_errno=errno; | 
 
 
 
 
 | 84 | > |  | 
 
 
 
 
 | 85 | > | if(level<=ihost_config.verbose){ | 
 
 
 
 
 | 86 | > | va_start(ap, format); | 
 
 
 
 
 | 87 | > | vfprintf(ihost_config.log, format, ap); | 
 
 
 
 
 | 88 | > | va_end(ap); | 
 
 
 
 
 | 89 | > | if(level==LOG_CRIT){ | 
 
 
 
 
 | 90 | > | fprintf(ihost_config.log, " (%s)\n", strerror(cur_errno)); | 
 
 
 
 
 | 91 | > | }else{ | 
 
 
 
 
 | 92 | > | fprintf(ihost_config.log, "\n"); | 
 
 
 
 
 | 93 | > | } | 
 
 
 
 
 
 
 
 
 
 
 | 94 |  | } | 
 
 
 
 
 
 
 
 | 95 | + | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 96 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 97 | < | memset(&addr, 0, sizeof addr); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 98 | < | addr.sin_family = AF_INET; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 99 | < | memcpy(&addr.sin_addr, &haddr, sizeof haddr); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 100 | < | addr.sin_port =  htons(ihost_state->fm_port); | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 97 | > | /* Takes many pointers, checks if they are NULL or not, and then free's them */ | 
 
 
 
 
 | 98 | > | /* Deprciated - and i only wrote it today! :) | 
 
 
 
 
 | 99 | > | void m_free(int num_pointers, ...){ | 
 
 
 
 
 | 100 | > | int x=0; | 
 
 
 
 
 | 101 | > | va_list ap; | 
 
 
 
 
 | 102 | > | void *p; | 
 
 
 
 
 
 
 
 
 
 
 | 103 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 104 | < | if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) { | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 105 | < | errf("Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 106 | < | return -1; | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 104 | > | va_start(ap, num_pointers); | 
 
 
 
 
 | 105 | > | for(;x<num_pointers;x++){ | 
 
 
 
 
 | 106 | > | p=va_arg(ap, void*); | 
 
 
 
 
 | 107 | > | if(p!=NULL){ | 
 
 
 
 
 | 108 | > | free(p); | 
 
 
 
 
 | 109 | > | } | 
 
 
 
 
 
 
 
 
 
 
 | 110 |  | } | 
 
 
 
 
 
 
 
 | 111 | + | va_end(ap); | 
 
 
 
 
 
 
 
 | 112 | + | } | 
 
 
 
 
 
 
 
 | 113 | + | */ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 114 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 115 | < | /* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 116 | < | if ((fm_fd_r=fdopen(sd,"r")) == NULL){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 117 | < | errf("Failed to open read stream (%m)"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 118 | < | return -1; | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 115 | > | int create_udp_sockinfo(udp_sockinfo_t *udp_sockinfo, char *hostname, int port){ | 
 
 
 
 
 | 116 | > |  | 
 
 
 
 
 | 117 | > | struct in_addr haddr; | 
 
 
 
 
 | 118 | > |  | 
 
 
 
 
 | 119 | > | log_msg(LOG_DEBUG, "Resolving name for udp connection"); | 
 
 
 
 
 | 120 | > | if(get_host_addr(hostname, &haddr) != 0){ | 
 
 
 
 
 | 121 | > | log_msg(LOG_CRIT, "Failed to lookup name"); | 
 
 
 
 
 | 122 | > | return 1; | 
 
 
 
 
 
 
 
 
 
 
 | 123 |  | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 124 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 125 | < | if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 126 | < | errf("Failed to open write stream (%m)"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 127 | < | return -1; | 
 
 
 
 
 
 
 
 
 | 125 | > | if((udp_sockinfo->sock=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){ | 
 
 
 
 
 | 126 | > | log_msg(LOG_CRIT, "Failed to create UDP socket"); | 
 
 
 
 
 | 127 | > | return 1; | 
 
 
 
 
 
 
 
 
 
 
 | 128 |  | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 129 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 130 | < | reply=sock_comm(fm_fd_r, fm_fd_w, "STARTCONFIG"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 131 | < | if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 132 | < | errf("Server error"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 133 | < | return -1; | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 130 | > | memset(&(udp_sockinfo->addr), 0, sizeof(struct sockaddr_in)); | 
 
 
 
 
 | 131 | > | udp_sockinfo->addr.sin_family=AF_INET; | 
 
 
 
 
 | 132 | > | memcpy((char *)&(udp_sockinfo->addr.sin_addr), &haddr, sizeof(haddr)); | 
 
 
 
 
 | 133 | > | udp_sockinfo->addr.sin_port = htons(port); | 
 
 
 
 
 | 134 | > |  | 
 
 
 
 
 | 135 | > | log_msg(LOG_DEBUG, "Socket created"); | 
 
 
 
 
 | 136 | > | return 0; | 
 
 
 
 
 | 137 | > | } | 
 
 
 
 
 | 138 | > |  | 
 
 
 
 
 | 139 | > | FILE *create_tcp_connection(char *hostname, int port){ | 
 
 
 
 
 | 140 | > | int sock; | 
 
 
 
 
 | 141 | > | struct sockaddr_in addr; | 
 
 
 
 
 | 142 | > | struct in_addr haddr; | 
 
 
 
 
 | 143 | > | FILE *f; | 
 
 
 
 
 | 144 | > |  | 
 
 
 
 
 | 145 | > | log_msg(LOG_DEBUG, "Creating tcp socket"); | 
 
 
 
 
 | 146 | > | if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))<0){ | 
 
 
 
 
 | 147 | > | log_msg(LOG_CRIT, "Failed to make TCP Socket"); | 
 
 
 
 
 | 148 | > | return NULL; | 
 
 
 
 
 
 
 
 
 
 
 | 149 |  | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 150 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 151 | < | reply=sock_comm(fm_fd_r, fm_fd_w, "LASTMODIFIED"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 152 | < | if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 153 | < | errf("Server error (%m)"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 114 | < | return -1; | 
 
 
 
 
 
 
 
 
 | 151 | > | if((get_host_addr(hostname, &haddr))!=0){ | 
 
 
 
 
 | 152 | > | log_msg(LOG_CRIT, "Failed to lookup name for %s", hostname); | 
 
 
 
 
 | 153 | > | return NULL; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 154 |  | } | 
 
 
 
 
 
 
 
 
 
 
 | 155 | < | if((ihost_state->last_modified=strdup(reply)) == NULL){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 156 | < | errf("strdup failed (%m)"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 157 | < | return -1; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 158 | < | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 159 | < |  | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 160 | < | reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 161 | < | if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 162 | < | errf("Server error (%m)"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 163 | < | return -1; | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 155 | > |  | 
 
 
 
 
 | 156 | > | memset(&addr, 0, sizeof(addr)); | 
 
 
 
 
 | 157 | > | addr.sin_family = AF_INET; | 
 
 
 
 
 | 158 | > | memcpy(&addr.sin_addr, &haddr, sizeof(haddr)); | 
 
 
 
 
 | 159 | > | addr.sin_port = htons(port); | 
 
 
 
 
 | 160 | > |  | 
 
 
 
 
 | 161 | > | log_msg(LOG_DEBUG, "Creating a tcp connection"); | 
 
 
 
 
 | 162 | > | if(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) !=0){ | 
 
 
 
 
 | 163 | > | log_msg(LOG_CRIT, "Failed to connect to hostname %s on port %d", hostname, port); | 
 
 
 
 
 | 164 | > | return NULL; | 
 
 
 
 
 
 
 
 
 
 
 | 165 |  | } | 
 
 
 
 
 
 
 
 
 | 126 | – | if((ihost_state->files_list=strdup(reply)) == NULL){ | 
 
 
 
 
 
 
 
 
 | 127 | – | errf("strdup failed (%m)"); | 
 
 
 
 
 
 
 
 
 | 128 | – | return -1; | 
 
 
 
 
 
 
 
 
 | 129 | – | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 166 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 167 | < | reply=sock_comm(fm_fd_r, fm_fd_w, "FQDN"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 168 | < | if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 169 | < | errf("Server error (%m)"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 170 | < | return -1; | 
 
 
 
 
 
 
 
 
 | 167 | > | if((f=fdopen(sock, "r+"))==NULL){ | 
 
 
 
 
 | 168 | > | log_msg(LOG_CRIT, "Failed to connect to open filedescriptor on tcp connection"); | 
 
 
 
 
 | 169 | > | close(sock); | 
 
 
 
 
 | 170 | > | return NULL; | 
 
 
 
 
 
 
 
 
 
 
 | 171 |  | } | 
 
 
 
 
 
 
 
 
 
 
 | 172 | < | if((ihost_state->my_fqdn=strdup(reply)) == NULL){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 173 | < | errf("strdup failed (%m)"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 172 | > |  | 
 
 
 
 
 | 173 | > | return f; | 
 
 
 
 
 | 174 | > | } | 
 
 
 
 
 | 175 | > |  | 
 
 
 
 
 | 176 | > | int tcp_comm(FILE *f, char *send, char **response, char *expected){ | 
 
 
 
 
 | 177 | > |  | 
 
 
 
 
 | 178 | > | log_msg(LOG_DEBUG, "Sending %s", send); | 
 
 
 
 
 | 179 | > | fprintf(f, "%s\n", send); | 
 
 
 
 
 | 180 | > | fflush(f); | 
 
 
 
 
 | 181 | > | *response=fpgetline(f); | 
 
 
 
 
 | 182 | > | fseek(f, 0, SEEK_CUR); | 
 
 
 
 
 | 183 | > |  | 
 
 
 
 
 | 184 | > | if(*response!=NULL) log_msg(LOG_DEBUG, "Recieved %s", *response); | 
 
 
 
 
 | 185 | > |  | 
 
 
 
 
 | 186 | > | if( (*response==NULL) || (strcmp(*response, "ERROR")==0) ) return -1; | 
 
 
 
 
 | 187 | > |  | 
 
 
 
 
 | 188 | > | if(expected==NULL) return 0; | 
 
 
 
 
 | 189 | > |  | 
 
 
 
 
 | 190 | > | if((strcmp(expected, *response))==0) return 0; | 
 
 
 
 
 | 191 | > |  | 
 
 
 
 
 | 192 | > | log_msg(LOG_DEBUG, "Did not get expected response"); | 
 
 
 
 
 | 193 | > | return -1; | 
 
 
 
 
 | 194 | > | } | 
 
 
 
 
 | 195 | > |  | 
 
 
 
 
 | 196 | > | int tcp_comm_strdup(FILE *f, char *send, char **response, char *expected){ | 
 
 
 
 
 | 197 | > | if((tcp_comm(f, send, response, expected))!=0){ | 
 
 
 
 
 
 
 
 
 
 
 | 198 |  | return -1; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 199 |  | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 200 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 201 | < | reply=sock_comm(fm_fd_r, fm_fd_w, "UDPUpdateTime"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 202 | < | if(reply== NULL){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 203 | < | errf("Server error (%m)"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 204 | < | return -1; | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 201 | > | *response=strdup(*response); | 
 
 
 
 
 | 202 | > | if (response==NULL) return -1; | 
 
 
 
 
 | 203 | > |  | 
 
 
 
 
 | 204 | > | return 0; | 
 
 
 
 
 | 205 | > | } | 
 
 
 
 
 | 206 | > |  | 
 
 
 
 
 | 207 | > | int ihost_getconfig(ihost_state_t *ihost_state){ | 
 
 
 
 
 | 208 | > |  | 
 
 
 
 
 | 209 | > | FILE *tcp_con; | 
 
 
 
 
 | 210 | > | char *response; | 
 
 
 
 
 | 211 | > | char *response_ptr; | 
 
 
 
 
 | 212 | > |  | 
 
 
 
 
 | 213 | > | /* Keep these in case of a failure and so it can keep running on the old config */ | 
 
 
 
 
 | 214 | > | char *file_list=NULL; | 
 
 
 
 
 | 215 | > | char *last_modified=NULL; | 
 
 
 
 
 | 216 | > | char *host_fqdn=NULL; | 
 
 
 
 
 | 217 | > | char *host_ip=NULL; | 
 
 
 
 
 | 218 | > | int udp_update_time=0; | 
 
 
 
 
 | 219 | > | char *server_fqdn=NULL; | 
 
 
 
 
 | 220 | > | int server_udp_port=0; | 
 
 
 
 
 | 221 | > | time_t config_ttl=0; | 
 
 
 
 
 | 222 | > |  | 
 
 
 
 
 | 223 | > | if((tcp_con=create_tcp_connection(ihost_state->filtermanager_host, ihost_state->filtermanager_port))==NULL){ | 
 
 
 
 
 | 224 | > | goto error; | 
 
 
 
 
 
 
 
 
 
 
 | 225 |  | } | 
 
 
 
 
 
 
 
 
 | 146 | – | if (strncasecmp(reply, "ERROR", 5) != 0){ | 
 
 
 
 
 
 
 
 
 | 147 | – | ihost_state->udp_update_time=atoi(reply); | 
 
 
 
 
 
 
 
 
 | 148 | – | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 226 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 227 | < | reply=sock_comm(fm_fd_r, fm_fd_w, "TCPUpdateTime"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 228 | < | if(reply== NULL){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 229 | < | errf("Server error (%m)"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 230 | < | return -1; | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 227 | > | if(ihost_state->file_list!=NULL || ihost_state->last_modified!=NULL){ | 
 
 
 
 
 | 228 | > | if(tcp_con==NULL){ | 
 
 
 
 
 | 229 | > | goto error; | 
 
 
 
 
 | 230 | > | } | 
 
 
 
 
 | 231 | > |  | 
 
 
 
 
 | 232 | > | if((tcp_comm(tcp_con, "CHECKCONFIG", &response, "OK"))!=0){ | 
 
 
 
 
 | 233 | > | goto error; | 
 
 
 
 
 | 234 | > | } | 
 
 
 
 
 | 235 | > |  | 
 
 
 
 
 | 236 | > | if((tcp_comm(tcp_con, ihost_state->file_list, &response, "OK"))!=0){ | 
 
 
 
 
 | 237 | > | goto error; | 
 
 
 
 
 | 238 | > | } | 
 
 
 
 
 | 239 | > |  | 
 
 
 
 
 | 240 | > | if((tcp_comm(tcp_con, ihost_state->last_modified, &response, "OK"))==0){ | 
 
 
 
 
 | 241 | > | if((tcp_comm(tcp_con, "END", &response, "OK"))==0){ | 
 
 
 
 
 | 242 | > | goto error; | 
 
 
 
 
 | 243 | > | } | 
 
 
 
 
 | 244 | > | fclose(tcp_con); | 
 
 
 
 
 | 245 | > | return 0; | 
 
 
 
 
 | 246 | > | }else{ | 
 
 
 
 
 | 247 | > | if((strcmp(response, "EXPIRED"))!=0){ | 
 
 
 
 
 | 248 | > | goto error; | 
 
 
 
 
 | 249 | > | } | 
 
 
 
 
 | 250 | > | } | 
 
 
 
 
 
 
 
 
 
 
 | 251 |  | } | 
 
 
 
 
 
 
 
 
 | 155 | – | if (strncasecmp(reply, "ERROR", 5) != 0){ | 
 
 
 
 
 
 
 
 
 | 156 | – | ihost_state->tcp_update_time=atoi(reply); | 
 
 
 
 
 
 
 
 
 | 157 | – | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 252 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 253 | < | reply=sock_comm(fm_fd_r, fm_fd_w, "ENDCONFIG"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 254 | < | if(reply== NULL){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 255 | < | errf("Server error (%m)"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 256 | < | return -1; | 
 
 
 
 
 
 
 
 
 | 253 | > | /* If we got to here, the config must of expired */ | 
 
 
 
 
 | 254 | > |  | 
 
 
 
 
 | 255 | > | if((tcp_comm(tcp_con, "STARTCONFIG", &response, "OK"))!=0){ | 
 
 
 
 
 | 256 | > | goto error; | 
 
 
 
 
 
 
 
 
 
 
 | 257 |  | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 258 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 259 | < | reply=sock_comm(fm_fd_r, fm_fd_w, "FILTER"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 260 | < | if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 167 | < | errf("Server error FILTER failed (%m)"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 168 | < | return -1; | 
 
 
 
 
 
 
 
 
 | 259 | > | if((tcp_comm_strdup(tcp_con, "LASTMODIFIED", &response, NULL))!=0){ | 
 
 
 
 
 | 260 | > | goto error; | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 261 |  | } | 
 
 
 
 
 
 
 
 
 
 
 | 262 | < | reply_ptr=strchr(reply,';'); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 263 | < | if (reply_ptr==NULL){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 264 | < | errf("Incorrect data returned"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 265 | < | return -1; | 
 
 
 
 
 
 
 
 
 | 262 | > | last_modified=response; | 
 
 
 
 
 | 263 | > |  | 
 
 
 
 
 | 264 | > | if((tcp_comm_strdup(tcp_con, "FILELIST", &response, NULL))!=0){ | 
 
 
 
 
 | 265 | > | goto error; | 
 
 
 
 
 
 
 
 
 
 
 | 266 |  | } | 
 
 
 
 
 
 
 
 
 
 
 | 267 | < | *reply_ptr='\0'; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 268 | < | if((ihost_state->server_fqdn=strdup(reply)) == NULL){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 269 | < | errf("strdup failed (%m)"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 270 | < | return -1; | 
 
 
 
 
 
 
 
 
 | 267 | > | file_list=response; | 
 
 
 
 
 | 268 | > |  | 
 
 
 
 
 | 269 | > | if((tcp_comm_strdup(tcp_con, "FQDN", &response, NULL))!=0){ | 
 
 
 
 
 | 270 | > | goto error; | 
 
 
 
 
 
 
 
 
 
 
 | 271 |  | } | 
 
 
 
 
 
 
 
 
 
 
 | 272 | < | reply=reply_ptr + 1; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 273 | < | reply_ptr=strchr(reply,';'); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 274 | < | if (reply_ptr==NULL){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 275 | < | errf("Incorrect data returned 2"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 184 | < | return -1; | 
 
 
 
 
 
 
 
 
 | 272 | > | host_fqdn=response; | 
 
 
 
 
 | 273 | > |  | 
 
 
 
 
 | 274 | > | if((tcp_comm_strdup(tcp_con, "IP", &response, NULL))!=0){ | 
 
 
 
 
 | 275 | > | goto error; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 276 |  | } | 
 
 
 
 
 
 
 
 
 
 
 | 277 | < | *reply_ptr='\0'; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 278 | < | ihost_state->server_udp_port=atoi(reply); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 279 | < | reply=reply_ptr+1; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 280 | < | ihost_state->server_tcp_port=atoi(reply); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 190 | < | if ((ihost_state->server_tcp_port==0) || (ihost_state->server_udp_port==0)){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 191 | < | errf("Incorrect data returned 3 "); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 192 | < | return -1; | 
 
 
 
 
 
 
 
 
 | 277 | > | host_ip=response; | 
 
 
 
 
 | 278 | > |  | 
 
 
 
 
 | 279 | > | if((tcp_comm(tcp_con, "UDPUpdateTime", &response, NULL))!=0){ | 
 
 
 
 
 | 280 | > | goto error; | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 281 |  | } | 
 
 
 
 
 
 
 
 | 282 | + | udp_update_time=atoi(response); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 283 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 284 | < | reply=sock_comm(fm_fd_r, fm_fd_w, "END"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 285 | < | if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 197 | < | errf("Server error (%m)"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 198 | < | return -1; | 
 
 
 
 
 
 
 
 
 | 284 | > | if((tcp_comm(tcp_con, "ConfigTTL", &response, NULL))!=0){ | 
 
 
 
 
 | 285 | > | goto error; | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 286 |  | } | 
 
 
 
 
 
 
 
 | 287 | + | config_ttl=atoi(response); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 288 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 289 | < | if(fclose(fm_fd_r) !=0){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 290 | < | errf("Failed to close read FD (%m)"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 203 | < | return -1; | 
 
 
 
 
 
 
 
 
 | 289 | > | if((tcp_comm(tcp_con, "ENDCONFIG", &response, NULL))!=0){ | 
 
 
 
 
 | 290 | > | goto error; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 291 |  | } | 
 
 
 
 
 
 
 
 
 
 
 | 292 | < | if(fclose(fm_fd_w) !=0){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 293 | < | errf("Failed to close write FD (%m)"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 294 | < | return -1; | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 292 | > |  | 
 
 
 
 
 | 293 | > | if((tcp_comm(tcp_con, "FILTER", &response, NULL))!=0){ | 
 
 
 
 
 | 294 | > | goto error; | 
 
 
 
 
 | 295 | > | }else{ | 
 
 
 
 
 | 296 | > | response_ptr=strchr(response,';'); | 
 
 
 
 
 | 297 | > | if(response_ptr==NULL){ | 
 
 
 
 
 | 298 | > | log_msg(LOG_ERR, "Incorrect data sent by server"); | 
 
 
 
 
 | 299 | > | goto error; | 
 
 
 
 
 | 300 | > | } | 
 
 
 
 
 | 301 | > | *response_ptr='\0'; | 
 
 
 
 
 | 302 | > | server_fqdn=strdup(response); | 
 
 
 
 
 | 303 | > | if(server_fqdn==NULL){ | 
 
 
 
 
 | 304 | > | goto error; | 
 
 
 
 
 | 305 | > | } | 
 
 
 
 
 | 306 | > | response_ptr++; | 
 
 
 
 
 | 307 | > | if(response_ptr==NULL){ | 
 
 
 
 
 | 308 | > | log_msg(LOG_ERR, "Incorrect data sent by server"); | 
 
 
 
 
 | 309 | > | goto error; | 
 
 
 
 
 | 310 | > | } | 
 
 
 
 
 | 311 | > |  | 
 
 
 
 
 | 312 | > | printf("string : %s\n", response_ptr); | 
 
 
 
 
 | 313 | > | server_udp_port=atoi(response_ptr); | 
 
 
 
 
 | 314 | > |  | 
 
 
 
 
 | 315 | > | if (server_udp_port==0){ | 
 
 
 
 
 | 316 | > | log_msg(LOG_ERR, "Incorrect data sent by server"); | 
 
 
 
 
 | 317 | > | goto error; | 
 
 
 
 
 | 318 | > | } | 
 
 
 
 
 
 
 
 
 
 
 | 319 |  | } | 
 
 
 
 
 
 
 
 | 320 | + |  | 
 
 
 
 
 
 
 
 | 321 | + | if((tcp_comm(tcp_con, "END", &response, "OK"))!=0){ | 
 
 
 
 
 
 
 
 | 322 | + | goto error; | 
 
 
 
 
 
 
 
 | 323 | + | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 324 |  |  | 
 
 
 
 
 
 
 
 | 325 | + | fclose(tcp_con); | 
 
 
 
 
 
 
 
 | 326 | + |  | 
 
 
 
 
 
 
 
 | 327 | + | /* We have the data we need, and its all been read correctly */ | 
 
 
 
 
 
 
 
 | 328 | + |  | 
 
 
 
 
 
 
 
 | 329 | + | /* Free the old data before pointing them to the new data. m_free copes should | 
 
 
 
 
 
 
 
 | 330 | + | * this already be NULL */ | 
 
 
 
 
 
 
 
 | 331 | + | if(ihost_state->file_list!=NULL) free(ihost_state->file_list); | 
 
 
 
 
 
 
 
 | 332 | + | if(ihost_state->last_modified!=NULL) free(ihost_state->last_modified); | 
 
 
 
 
 
 
 
 | 333 | + | if(ihost_state->host_fqdn!=NULL) free(ihost_state->host_fqdn); | 
 
 
 
 
 
 
 
 | 334 | + | if(ihost_state->server_fqdn!=NULL) free(ihost_state->server_fqdn); | 
 
 
 
 
 
 
 
 | 335 | + | if(ihost_state->host_ip!=NULL) free(ihost_state->host_ip); | 
 
 
 
 
 
 
 
 | 336 | + |  | 
 
 
 
 
 
 
 
 | 337 | + | ihost_state->file_list=file_list; | 
 
 
 
 
 
 
 
 | 338 | + | ihost_state->last_modified=last_modified; | 
 
 
 
 
 
 
 
 | 339 | + | ihost_state->host_fqdn=host_fqdn; | 
 
 
 
 
 
 
 
 | 340 | + | ihost_state->host_ip=host_ip; | 
 
 
 
 
 
 
 
 | 341 | + | ihost_state->server_fqdn=server_fqdn; | 
 
 
 
 
 
 
 
 | 342 | + | ihost_state->server_udp_port=server_udp_port; | 
 
 
 
 
 
 
 
 | 343 | + | ihost_state->udp_update_time=udp_update_time; | 
 
 
 
 
 
 
 
 | 344 | + | ihost_state->config_ttl=config_ttl; | 
 
 
 
 
 
 
 
 | 345 | + |  | 
 
 
 
 
 
 
 
 | 346 | + | log_msg(LOG_DEBUG, "UDP Update time %d", udp_update_time); | 
 
 
 
 
 
 
 
 | 347 | + | log_msg(LOG_DEBUG, "Configure ttl %d", config_ttl); | 
 
 
 
 
 
 
 
 | 348 | + |  | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 349 |  | return 0; | 
 
 
 
 
 
 
 
 | 350 | + |  | 
 
 
 
 
 
 
 
 | 351 | + | error: | 
 
 
 
 
 
 
 
 | 352 | + |  | 
 
 
 
 
 
 
 
 | 353 | + | if(file_list!=NULL) free(file_list); | 
 
 
 
 
 
 
 
 | 354 | + | if(last_modified!=NULL) free(last_modified); | 
 
 
 
 
 
 
 
 | 355 | + | if(host_fqdn!=NULL) free(host_fqdn); | 
 
 
 
 
 
 
 
 | 356 | + | if(server_fqdn!=NULL) free(server_fqdn); | 
 
 
 
 
 
 
 
 | 357 | + | if(host_ip!=NULL) free(host_ip); | 
 
 
 
 
 
 
 
 | 358 | + | fclose(tcp_con); | 
 
 
 
 
 
 
 
 | 359 | + |  | 
 
 
 
 
 
 
 
 | 360 | + | return -1; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 361 |  | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 362 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 363 | < | int heartbeat(ihost_state_t *ihost_state){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 364 | < | struct sockaddr_in addr; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 365 | < | struct in_addr haddr; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 366 | < | int sd; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 367 | < | FILE *fm_fd_r, *fm_fd_w; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 368 | < | char *reply; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 369 | < | int exitcode=0; | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 363 | > | int get_system_stats(int seq_no, ihost_state_t *ihost_state, char *xml, int size){ | 
 
 
 
 
 | 364 | > | char tmp[size]; | 
 
 
 
 
 | 365 | > | cpu_percent_t *cpu_percent; | 
 
 
 
 
 | 366 | > | mem_stat_t *mem_stats; | 
 
 
 
 
 | 367 | > | load_stat_t *load_stats; | 
 
 
 
 
 | 368 | > | user_stat_t *user_stats; | 
 
 
 
 
 | 369 | > | swap_stat_t *swap_stats; | 
 
 
 
 
 | 370 | > | general_stat_t *general_stats; | 
 
 
 
 
 | 371 | > | disk_stat_t *disk_stats; | 
 
 
 
 
 | 372 | > | diskio_stat_t *diskio_stats; | 
 
 
 
 
 | 373 | > | process_stat_t *process_stats; | 
 
 
 
 
 | 374 | > | network_stat_t *network_stats; | 
 
 
 
 
 | 375 | > | page_stat_t *page_stats; | 
 
 
 
 
 | 376 | > | int disk_entries=0; | 
 
 
 
 
 | 377 | > | int diskio_entries=0; | 
 
 
 
 
 | 378 | > | int network_entries=0; | 
 
 
 
 
 
 
 
 
 
 
 | 379 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 380 | < | if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) { | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 381 | < | errf("Can't create AF_INET socket (%m)"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 382 | < | return -1; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 224 | < | } | 
 
 
 
 
 
 
 
 
 | 380 | > | int counter; | 
 
 
 
 
 | 381 | > | long long x; | 
 
 
 
 
 | 382 | > | long long y; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 383 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 384 | < | if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 385 | < | errf("Failed to resolve address %s (%m)", ihost_state->fm_host); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 386 | < | return -1; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 229 | < | } | 
 
 
 
 
 
 
 
 
 | 384 | > | /* Print start of the packet we want */ | 
 
 
 
 
 | 385 | > | snprintf(xml, size, "<packet seq_no=\"%d\" machine_name=\"%s\" date=\"%ld\" type=\"data\" ip=\"%s\">", \ | 
 
 
 
 
 | 386 | > | seq_no, ihost_state->host_fqdn, time(NULL), ihost_state->host_ip); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 387 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 388 | < | memset(&addr, 0, sizeof addr); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 389 | < | addr.sin_family = AF_INET; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 390 | < | memcpy(&addr.sin_addr, &haddr, sizeof haddr); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 391 | < | addr.sin_port =  htons(ihost_state->server_tcp_port); | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 388 | > | /* Get cpu stats, check it is correct, then fill in its entry for the xml */ | 
 
 
 
 
 | 389 | > | if((cpu_percent=cpu_percent_usage())==NULL){ | 
 
 
 
 
 | 390 | > | log_msg(LOG_CRIT, "Failed to get cpu statistics"); | 
 
 
 
 
 | 391 | > | }else{ | 
 
 
 
 
 | 392 | > | snprintf(tmp, size, \ | 
 
 
 
 
 | 393 | > | "<cpu><user>%3.2f</user><kernel>%3.2f</kernel><idle>%3.2f</idle><iowait>%3.2f</iowait><swap>%3.2f</swap></cpu>", \ | 
 
 
 
 
 | 394 | > | cpu_percent->user, \ | 
 
 
 
 
 | 395 | > | cpu_percent->kernel, \ | 
 
 
 
 
 | 396 | > | cpu_percent->idle, \ | 
 
 
 
 
 | 397 | > | cpu_percent->iowait, \ | 
 
 
 
 
 | 398 | > | cpu_percent->swap); | 
 
 
 
 
 
 
 
 
 
 
 | 399 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 400 | < | if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) { | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 401 | < | errf("Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 238 | < | return -1; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 239 | < | } | 
 
 
 
 
 
 
 
 
 | 400 | > | if(strlcat(xml, tmp, size) >= size) goto too_big_error; | 
 
 
 
 
 | 401 | > | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 402 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 403 | < | /* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 404 | < | if ((fm_fd_r=fdopen(sd,"r")) == NULL){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 405 | < | errf("Failed to open stream (%m)"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 406 | < | return -1; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 407 | < | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 403 | > |  | 
 
 
 
 
 | 404 | > | /*Get mem stats, and fill in xml */ | 
 
 
 
 
 | 405 | > | if((mem_stats=get_memory_stats())==NULL){ | 
 
 
 
 
 | 406 | > | log_msg(LOG_CRIT, "Failed to get memory statistics"); | 
 
 
 
 
 | 407 | > | }else{ | 
 
 
 
 
 | 408 | > | snprintf(tmp, size, \ | 
 
 
 
 
 | 409 | > | "<memory><total>%lld</total><free>%lld</free><used>%lld</used><cache>%lld</cache></memory>", \ | 
 
 
 
 
 | 410 | > | mem_stats->total, \ | 
 
 
 
 
 | 411 | > | mem_stats->free, \ | 
 
 
 
 
 | 412 | > | mem_stats->used, \ | 
 
 
 
 
 | 413 | > | mem_stats->cache); | 
 
 
 
 
 | 414 | > |  | 
 
 
 
 
 | 415 | > | if(strlcat(xml, tmp, size) >= size) goto too_big_error; | 
 
 
 
 
 | 416 | > | } | 
 
 
 
 
 
 
 
 
 
 
 | 417 |  |  | 
 
 
 
 
 
 
 
 
 | 247 | – | if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){ | 
 
 
 
 
 
 
 
 
 | 248 | – | errf("Failed to open stream (%m)"); | 
 
 
 
 
 
 
 
 
 | 249 | – | return -1; | 
 
 
 
 
 
 
 
 
 | 250 | – | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 418 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 419 | < | reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 420 | < | if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 421 | < | errf("Server error"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 422 | < | return -1; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 423 | < | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 419 | > | /* Get load stats */ | 
 
 
 
 
 | 420 | > | if((load_stats=get_load_stats())==NULL){ | 
 
 
 
 
 | 421 | > | log_msg(LOG_CRIT, "Failed to get load statistics"); | 
 
 
 
 
 | 422 | > | }else{ | 
 
 
 
 
 | 423 | > | snprintf(tmp, size, \ | 
 
 
 
 
 | 424 | > | "<load><load1>%.2lf</load1><load5>%.2lf</load5><load15>%.2lf</load15></load>", \ | 
 
 
 
 
 | 425 | > | load_stats->min1, \ | 
 
 
 
 
 | 426 | > | load_stats->min5, \ | 
 
 
 
 
 | 427 | > | load_stats->min15); | 
 
 
 
 
 | 428 | > | if(strlcat(xml, tmp, size) >= size) goto too_big_error; | 
 
 
 
 
 | 429 | > | } | 
 
 
 
 
 
 
 
 
 
 
 | 430 |  |  | 
 
 
 
 
 
 
 
 
 | 258 | – | reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG"); | 
 
 
 
 
 
 
 
 
 | 259 | – | if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { | 
 
 
 
 
 
 
 
 
 | 260 | – | errf("Server error"); | 
 
 
 
 
 
 
 
 
 | 261 | – | return -1; | 
 
 
 
 
 
 
 
 
 | 262 | – | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 431 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 432 | < | reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->files_list); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 433 | < | if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 434 | < | errf("Server error"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 435 | < | return -1; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 436 | < | } | 
 
 
 
 
 
 
 
 
 | 432 | > | /* get user stats */ | 
 
 
 
 
 | 433 | > |  | 
 
 
 
 
 | 434 | > | if((user_stats=get_user_stats())==NULL){ | 
 
 
 
 
 | 435 | > | log_msg(LOG_CRIT, "Failed to get user statistics"); | 
 
 
 
 
 | 436 | > | }else{ | 
 
 
 
 
 
 
 
 
 
 
 | 437 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 438 | < | reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->last_modified); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 439 | < | if (reply==NULL) { | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 440 | < | errf("Server error"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 441 | < | return -1; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 442 | < | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 443 | < | if (strncasecmp(reply, "ERROR", 5) == 0){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 276 | < | /* Means the config has changed */ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 277 | < | exitcode=RECONFIGURE_RETURN_CODE; | 
 
 
 
 
 
 
 
 
 | 438 | > | snprintf(tmp, size, \ | 
 
 
 
 
 | 439 | > | "<users><list>%s</list><count>%d</count></users>", \ | 
 
 
 
 
 | 440 | > | user_stats->name_list, \ | 
 
 
 
 
 | 441 | > | user_stats->num_entries); | 
 
 
 
 
 | 442 | > |  | 
 
 
 
 
 | 443 | > | if(strlcat(xml, tmp, size) >= size) goto too_big_error; | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 444 |  | } | 
 
 
 
 
 
 
 
 
 | 279 | – | reply=sock_comm(fm_fd_r, fm_fd_w, "KEY"); | 
 
 
 
 
 
 
 
 
 | 280 | – | if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { | 
 
 
 
 
 
 
 
 
 | 281 | – | errf("Server error"); | 
 
 
 
 
 
 
 
 
 | 282 | – | return -1; | 
 
 
 
 
 
 
 
 
 | 283 | – | } | 
 
 
 
 
 
 
 
 
 | 284 | – | if (ihost_state->key!=NULL) free(ihost_state->key); | 
 
 
 
 
 
 
 
 
 | 285 | – |  | 
 
 
 
 
 
 
 
 
 | 286 | – | if((ihost_state->key=strdup(reply)) == NULL){ | 
 
 
 
 
 
 
 
 
 | 287 | – | errf("strdup failed (%m)"); | 
 
 
 
 
 
 
 
 
 | 288 | – | return -1; | 
 
 
 
 
 
 
 
 
 | 289 | – | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 445 |  |  | 
 
 
 
 
 
 
 
 
 | 291 | – | reply=sock_comm(fm_fd_r, fm_fd_w, "ENDHEARTBEAT"); | 
 
 
 
 
 
 
 
 
 | 292 | – | if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ | 
 
 
 
 
 
 
 
 
 | 293 | – | errf("Server error (%m)"); | 
 
 
 
 
 
 
 
 
 | 294 | – | return -1; | 
 
 
 
 
 
 
 
 
 | 295 | – | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 446 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 447 | < | fflush(fm_fd_r); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 448 | < | fflush(fm_fd_w); | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 447 | > | /* swap stats */ | 
 
 
 
 
 | 448 | > | if((swap_stats=get_swap_stats())==NULL){ | 
 
 
 
 
 | 449 | > | log_msg(LOG_CRIT, "Failed to get swap statistics"); | 
 
 
 
 
 | 450 | > | }else{ | 
 
 
 
 
 | 451 | > | snprintf(tmp, size, \ | 
 
 
 
 
 | 452 | > | "<swap><total>%lld</total><used>%lld</used><free>%lld</free></swap>",\ | 
 
 
 
 
 | 453 | > | swap_stats->total, \ | 
 
 
 
 
 | 454 | > | swap_stats->used, \ | 
 
 
 
 
 | 455 | > | swap_stats->free); | 
 
 
 
 
 | 456 | > |  | 
 
 
 
 
 | 457 | > | if(strlcat(xml, tmp, size) >= size) goto too_big_error; | 
 
 
 
 
 | 458 | > | } | 
 
 
 
 
 
 
 
 
 
 
 | 459 |  |  | 
 
 
 
 
 
 
 
 
 | 300 | – | if(fclose(fm_fd_r) !=0){ | 
 
 
 
 
 
 
 
 
 | 301 | – | errf("Failed to close read FD (%m)"); | 
 
 
 
 
 
 
 
 
 | 302 | – | return -1; | 
 
 
 
 
 
 
 
 
 | 303 | – | } | 
 
 
 
 
 
 
 
 
 | 304 | – | if(fclose(fm_fd_w) !=0){ | 
 
 
 
 
 
 
 
 
 | 305 | – | errf("Failed to close write FD (%m)"); | 
 
 
 
 
 
 
 
 
 | 306 | – | return -1; | 
 
 
 
 
 
 
 
 
 | 307 | – | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 460 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 461 | < | return exitcode; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 462 | < | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 461 | > | /* general stats */ | 
 
 
 
 
 | 462 | > |  | 
 
 
 
 
 | 463 | > | if((general_stats=get_general_stats())==NULL){ | 
 
 
 
 
 | 464 | > | log_msg(LOG_CRIT, "Failed to get general statistics"); | 
 
 
 
 
 | 465 | > | }else{ | 
 
 
 
 
 | 466 | > | snprintf(tmp, size, \ | 
 
 
 
 
 | 467 | > | "<os><name>%s</name><release>%s</release><version>%s</version><sysname>%s</sysname><platform>%s</platform><uptime>%ld</uptime></os>", \ | 
 
 
 
 
 | 468 | > | general_stats->os_name, \ | 
 
 
 
 
 | 469 | > | general_stats->os_release, \ | 
 
 
 
 
 | 470 | > | general_stats->os_version, \ | 
 
 
 
 
 | 471 | > | general_stats->hostname, \ | 
 
 
 
 
 | 472 | > | general_stats->platform, \ | 
 
 
 
 
 | 473 | > | (long)general_stats->uptime); | 
 
 
 
 
 | 474 | > |  | 
 
 
 
 
 | 475 | > | if(strlcat(xml, tmp, size) >= size) goto too_big_error; | 
 
 
 
 
 | 476 | > |  | 
 
 
 
 
 | 477 | > | } | 
 
 
 
 
 
 
 
 
 
 
 | 478 |  |  | 
 
 
 
 
 
 
 
 
 | 312 | – | char *stat_grab(ihost_state_t *ihost_state, int counter){ | 
 
 
 
 
 
 
 
 
 | 313 | – | #define NUM_STATS 9 | 
 
 
 
 
 
 
 
 
 | 314 | – | char *stats[NUM_STATS]; | 
 
 
 
 
 
 
 
 
 | 315 | – | char *xml_data=NULL; | 
 
 
 
 
 
 
 
 
 | 316 | – | char *xml_data_p; | 
 
 
 
 
 
 
 
 
 | 317 | – | int x=0; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 479 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 480 | < | stats[0]=get_cpu_stats(); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 481 | < | stats[1]=get_disk_stats(); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 482 | < | stats[2]=get_load_stats(); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 483 | < | stats[3]=get_memory_stats(); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 484 | < | stats[4]=get_os_info(); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 485 | < | stats[5]=get_page_stats(); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 486 | < | stats[6]=get_process_stats(); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 487 | < | stats[7]=get_swap_stats(); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 488 | < | stats[8]=get_user_stats(); | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 480 | > | /* process stats */ | 
 
 
 
 
 | 481 | > | if((process_stats=get_process_stats())==NULL){ | 
 
 
 
 
 | 482 | > | log_msg(LOG_CRIT, "Failed to get general statistics"); | 
 
 
 
 
 | 483 | > | }else{ | 
 
 
 
 
 | 484 | > | snprintf(tmp, size, \ | 
 
 
 
 
 | 485 | > | "<processes><sleeping>%d</sleeping><cpu>%d</cpu><zombie>%d</zombie><stopped>%d</stopped><total>%d</total></processes>",\ | 
 
 
 
 
 | 486 | > | process_stats->sleeping, \ | 
 
 
 
 
 | 487 | > | process_stats->running, \ | 
 
 
 
 
 | 488 | > | process_stats->zombie, \ | 
 
 
 
 
 | 489 | > | process_stats->stopped, \ | 
 
 
 
 
 | 490 | > | process_stats->total); | 
 
 
 
 
 
 
 
 
 
 
 | 491 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 492 | < | for(;x<NUM_STATS;x++){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 493 | < | if(stats[x]==NULL){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 494 | < | return NULL; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 495 | < | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 496 | < | if(xml_data==NULL){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 497 | < | if((xml_data=strf("%s", stats[x])) == NULL){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 498 | < | errf("str failed (%m)"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 499 | < | return NULL; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 500 | < | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 492 | > | if(strlcat(xml, tmp, size) >= size) goto too_big_error; | 
 
 
 
 
 | 493 | > |  | 
 
 
 
 
 | 494 | > | } | 
 
 
 
 
 | 495 | > |  | 
 
 
 
 
 | 496 | > |  | 
 
 
 
 
 | 497 | > | /* Get paging stats */ | 
 
 
 
 
 | 498 | > | if((page_stats=get_page_stats_diff())==NULL){ | 
 
 
 
 
 | 499 | > | log_msg(LOG_CRIT, "Failed to get paging statistics"); | 
 
 
 
 
 | 500 | > | }else{ | 
 
 
 
 
 | 501 | > | if(page_stats->systime!=0){ | 
 
 
 
 
 | 502 | > | x=page_stats->pages_pagein / page_stats->systime; | 
 
 
 
 
 | 503 | > | y=page_stats->pages_pageout / page_stats->systime; | 
 
 
 
 
 
 
 
 
 
 
 | 504 |  | }else{ | 
 
 
 
 
 
 
 
 
 
 
 | 505 | < | xml_data_p=xml_data; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 506 | < | if((xml_data=strf("%s%s", xml_data, stats[x])) == NULL){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 341 | < | errf("str failed (%m)"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 342 | < | return NULL; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 343 | < | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 344 | < | free(xml_data_p); | 
 
 
 
 
 
 
 
 
 | 505 | > | x=page_stats->pages_pagein; | 
 
 
 
 
 | 506 | > | y=page_stats->pages_pageout; | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 507 |  | } | 
 
 
 
 
 
 
 
 
 
 
 | 508 | < | free(stats[x]); | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 508 | > | snprintf(tmp, size, \ | 
 
 
 
 
 | 509 | > | "<pages><swapins>%lld</swapins><swapouts>%lld</swapouts></pages>", \ | 
 
 
 
 
 | 510 | > | x, \ | 
 
 
 
 
 | 511 | > | y); | 
 
 
 
 
 | 512 | > |  | 
 
 
 
 
 | 513 | > | if(strlcat(xml, tmp, size) >= size) goto too_big_error; | 
 
 
 
 
 
 
 
 
 
 
 | 514 |  | } | 
 
 
 
 
 
 
 
 
 
 
 | 515 | < | xml_data_p=xml_data; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 516 | < | xml_data=strf("<packet seq_no=\"%d\" machine_name=\"%s\" date=\"%ld\" type=\"data\" ip=\"%s\" key=\"%s\">%s</packet>", counter, ihost_state->my_fqdn, time(NULL), "127.0.0.1", ihost_state->key, xml_data); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 517 | < | free(xml_data_p); | 
 
 
 
 
 
 
 
 
 | 515 | > |  | 
 
 
 
 
 | 516 | > |  | 
 
 
 
 
 | 517 | > | /* get diskio stats */ | 
 
 
 
 
 
 
 
 
 
 
 | 518 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 519 | < | return xml_data; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 520 | < | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 519 | > | if((diskio_stats=get_diskio_stats_diff(&diskio_entries))==NULL){ | 
 
 
 
 
 | 520 | > | log_msg(LOG_CRIT, "Fahttp://www.cs.ukc.ac.uk/teaching/02/modules/CO/6/17/rdl/criteria.pdfiled to get diskio statistics"); | 
 
 
 
 
 | 521 | > | }else{ | 
 
 
 
 
 | 522 | > | strlcat(xml, "<diskio>", size); | 
 
 
 
 
 | 523 | > | for(counter=0;counter<diskio_entries;counter++){ | 
 
 
 
 
 
 
 
 
 
 
 | 524 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 525 | < | int send_stats(ihost_state_t *ihost_state, char *data_stream){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 526 | < | struct sockaddr_in addr; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 527 | < | struct in_addr haddr; | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 525 | > | if(diskio_stats->systime!=0){ | 
 
 
 
 
 | 526 | > | x=diskio_stats->read_bytes / diskio_stats->systime; | 
 
 
 
 
 | 527 | > | y=diskio_stats->write_bytes / diskio_stats->systime; | 
 
 
 
 
 | 528 | > | }else{ | 
 
 
 
 
 | 529 | > | x=diskio_stats->read_bytes; | 
 
 
 
 
 | 530 | > | y=diskio_stats->write_bytes; | 
 
 
 
 
 | 531 | > | } | 
 
 
 
 
 | 532 | > |  | 
 
 
 
 
 | 533 | > | snprintf(tmp, size, \ | 
 
 
 
 
 | 534 | > | "<p%d name=\"%s\" rbytes=\"%lld\" wbytes=\"%lld\"></p%d>", \ | 
 
 
 
 
 | 535 | > | counter, \ | 
 
 
 
 
 | 536 | > | diskio_stats->disk_name, \ | 
 
 
 
 
 | 537 | > | x, \ | 
 
 
 
 
 | 538 | > | y, \ | 
 
 
 
 
 | 539 | > | counter); | 
 
 
 
 
 
 
 
 
 
 
 | 540 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 541 | < | int sd; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 542 | < | size_t len; | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 541 | > | strlcat(xml, tmp, size); | 
 
 
 
 
 | 542 | > | diskio_stats++; | 
 
 
 
 
 | 543 | > | } | 
 
 
 
 
 
 
 
 
 
 
 | 544 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 545 | < | len=strlen(data_stream); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 546 | < | if(len>UDP_MAX_PACKET_SIZE){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 364 | < | errf("Too big to send to server. Please reconfigure client and server and recompile"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 365 | < | exit(1); | 
 
 
 
 
 
 
 
 
 | 545 | > | if(strlcat(xml, "</diskio>", size) >= size) goto too_big_error; | 
 
 
 
 
 | 546 | > |  | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 547 |  | } | 
 
 
 
 
 
 
 
 | 548 | + |  | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 549 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 550 | < | if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 551 | < | errf("Failed to resolve address %s (%m)", ihost_state->fm_host); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 552 | < | return -1; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 553 | < | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 550 | > | /* get networks stats */ | 
 
 
 
 
 | 551 | > |  | 
 
 
 
 
 | 552 | > | if((network_stats=get_network_stats_diff(&network_entries))==NULL){ | 
 
 
 
 
 | 553 | > | log_msg(LOG_CRIT, "Failed to get network statistics"); | 
 
 
 
 
 | 554 | > | }else{ | 
 
 
 
 
 | 555 | > | strlcat(xml, "<net>", size); | 
 
 
 
 
 | 556 | > | for(counter=0;counter<network_entries;counter++){ | 
 
 
 
 
 | 557 | > | if(network_stats->systime!=0){ | 
 
 
 
 
 | 558 | > | x=network_stats->rx / network_stats->systime; | 
 
 
 
 
 | 559 | > | y=network_stats->tx / network_stats->systime; | 
 
 
 
 
 | 560 | > | }else{ | 
 
 
 
 
 | 561 | > | x=network_stats->rx; | 
 
 
 
 
 | 562 | > | y=network_stats->tx; | 
 
 
 
 
 | 563 | > | } | 
 
 
 
 
 
 
 
 
 
 
 | 564 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 565 | < | if((sd=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 566 | < | errf("failed to create UDP socket (%m)"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 567 | < | return -1; | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 565 | > | snprintf(tmp, size, \ | 
 
 
 
 
 | 566 | > | "<p%d name=\"%s\" rx=\"%lld\" tx=\"%lld\"></p%d>", \ | 
 
 
 
 
 | 567 | > | counter, \ | 
 
 
 
 
 | 568 | > | network_stats->interface_name, \ | 
 
 
 
 
 | 569 | > | x, \ | 
 
 
 
 
 | 570 | > | y, \ | 
 
 
 
 
 | 571 | > | counter); | 
 
 
 
 
 | 572 | > |  | 
 
 
 
 
 | 573 | > | strlcat(xml, tmp, size); | 
 
 
 
 
 | 574 | > | network_stats++; | 
 
 
 
 
 | 575 | > | } | 
 
 
 
 
 | 576 | > |  | 
 
 
 
 
 | 577 | > | if(strlcat(xml, "</net>", size) >= size) goto too_big_error; | 
 
 
 
 
 | 578 | > |  | 
 
 
 
 
 
 
 
 
 
 
 | 579 |  | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 580 |  |  | 
 
 
 
 
 
 
 
 
 | 378 | – | memset(&addr, 0, sizeof(addr)); | 
 
 
 
 
 
 
 
 
 | 379 | – | addr.sin_family=AF_INET; | 
 
 
 
 
 
 
 
 
 | 380 | – | memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr); | 
 
 
 
 
 
 
 
 
 | 381 | – | addr.sin_port =  htons(ihost_state->server_udp_port); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 581 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 582 | < | if((sendto(sd, data_stream, len, 0, (struct sockaddr *) &addr, sizeof(addr))) != len){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 583 | < | errf("Send the wrong number of bytes (%m)"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 584 | < | return -1; | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 582 | > | /* get disk stats */ | 
 
 
 
 
 | 583 | > |  | 
 
 
 
 
 | 584 | > | if((disk_stats=get_disk_stats(&disk_entries))==NULL){ | 
 
 
 
 
 | 585 | > | log_msg(LOG_CRIT, "Failed to get disk statistics"); | 
 
 
 
 
 | 586 | > | }else{ | 
 
 
 
 
 | 587 | > | strlcat(xml, "<disk>", size); | 
 
 
 
 
 | 588 | > | for(counter=0;counter<disk_entries;counter++){ | 
 
 
 
 
 | 589 | > | snprintf(tmp, size, \ | 
 
 
 
 
 | 590 | > | "<p%d name=\"%s\" mount=\"%s\" fstype=\"%s\" total=\"%lld\" used=\"%lld\" avail=\"%lld\" totalinodes=\"%lld\" usedinodes=\"%lld\" freeinodes=\"%lld\"></p%d>", \ | 
 
 
 
 
 | 591 | > | counter, \ | 
 
 
 
 
 | 592 | > | disk_stats->device_name, \ | 
 
 
 
 
 | 593 | > | disk_stats->mnt_point, \ | 
 
 
 
 
 | 594 | > | disk_stats->fs_type, \ | 
 
 
 
 
 | 595 | > | disk_stats->size, \ | 
 
 
 
 
 | 596 | > | disk_stats->used, \ | 
 
 
 
 
 | 597 | > | disk_stats->avail, \ | 
 
 
 
 
 | 598 | > | disk_stats->total_inodes, \ | 
 
 
 
 
 | 599 | > | disk_stats->used_inodes, \ | 
 
 
 
 
 | 600 | > | disk_stats->free_inodes, \ | 
 
 
 
 
 | 601 | > | counter); | 
 
 
 
 
 | 602 | > |  | 
 
 
 
 
 | 603 | > | strlcat(xml, tmp, size); | 
 
 
 
 
 | 604 | > |  | 
 
 
 
 
 | 605 | > | disk_stats++; | 
 
 
 
 
 | 606 | > | } | 
 
 
 
 
 | 607 | > |  | 
 
 
 
 
 | 608 | > | if(strlcat(xml, "</disk>", size) >= size) goto too_big_error; | 
 
 
 
 
 | 609 | > |  | 
 
 
 
 
 
 
 
 
 
 
 | 610 |  | } | 
 
 
 
 
 
 
 
 | 611 | + |  | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 612 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 613 | < | close(sd); | 
 
 
 
 
 
 
 
 
 | 613 | > | if(strlcat(xml, "</packet>", size) >= size) goto too_big_error; | 
 
 
 
 
 
 
 
 
 
 
 | 614 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 615 | < | printf("%s\n",data_stream); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 616 | < | return 0; | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 615 | > | /*If we got to here, it should of all been filled in nicely now */ | 
 
 
 
 
 | 616 | > | return 0; | 
 
 
 
 
 | 617 | > |  | 
 
 
 
 
 | 618 | > | too_big_error: | 
 
 
 
 
 | 619 | > | log_msg(LOG_ERR, "UDP Packet is too large. Throwing away the packet"); | 
 
 
 
 
 | 620 | > | return -1; | 
 
 
 
 
 
 
 
 
 
 
 | 621 |  | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 622 |  |  | 
 
 
 
 
 
 
 
 | 623 | + |  | 
 
 
 
 
 
 
 
 | 624 | + |  | 
 
 
 
 
 
 
 
 | 625 | + | void usage(char *progname){ | 
 
 
 
 
 
 
 
 | 626 | + | fprintf(stderr, "Usage %s [options] server port\n", progname); | 
 
 
 
 
 
 
 
 | 627 | + | fprintf(stderr, "Options\n"); | 
 
 
 
 
 
 
 
 | 628 | + | fprintf(stderr, "  -v           Verbose mode,-vv would make even more verbose\n"); | 
 
 
 
 
 
 
 
 | 629 | + | fprintf(stderr, "  -f           Foreground mode, print errors to stderr\n"); | 
 
 
 
 
 
 
 
 | 630 | + | fprintf(stderr, "  -V           Print version number\n"); | 
 
 
 
 
 
 
 
 | 631 | + | fprintf(stderr, "  -h           Prints this help page\n"); | 
 
 
 
 
 
 
 
 | 632 | + | exit(1); | 
 
 
 
 
 
 
 
 | 633 | + | } | 
 
 
 
 
 
 
 
 | 634 | + |  | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 635 |  | int main(int argc, char **argv){ | 
 
 
 
 
 
 
 
 | 636 | + |  | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 637 |  | ihost_state_t ihost_state; | 
 
 
 
 
 
 
 
 
 
 
 | 638 | < | int heartbeat_exit; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 639 | < | int counter=0; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 640 | < | long udp_time=0, tcp_time=0, stat_grab_time=0, cur_time=0; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 641 | < | int sleep_delay=0; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 642 | < | char *xml_stats; | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 638 | > | udp_sockinfo_t udp_sockinfo; | 
 
 
 
 
 | 639 | > |  | 
 
 
 
 
 | 640 | > | int cmdopt; | 
 
 
 
 
 | 641 | > | extern int optind; | 
 
 
 
 
 | 642 | > | pid_t pid; | 
 
 
 
 
 | 643 | > | FILE *f; | 
 
 
 
 
 | 644 | > | int packet_num=0; | 
 
 
 
 
 | 645 | > | int len; | 
 
 
 
 
 
 
 
 
 
 
 | 646 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 647 | < | /* NULL'ify so i can tell if i need to free it or not */ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 403 | < | ihost_state.fm_host=NULL; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 404 | < | ihost_state.my_fqdn=NULL; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 405 | < | ihost_state.server_fqdn=NULL; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 406 | < | ihost_state.last_modified=NULL; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 407 | < | ihost_state.files_list=NULL; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 408 | < | ihost_state.key=NULL; | 
 
 
 
 
 
 
 
 
 | 647 | > | char packet[MAX_UDP_PACKET_SIZE]; | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 648 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 649 | < | errf_set_progname(argv[0]); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 650 | < | if(argc!=3){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 651 | < | errf_usage("<host> <port>"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 649 | > | time_t cur_time, sleep_delay, udp_time=0, config_time=0; | 
 
 
 
 
 | 650 | > |  | 
 
 
 
 
 | 651 | > | /* Set default settings */ | 
 
 
 
 
 | 652 | > | ihost_config.verbose=1; | 
 
 
 
 
 | 653 | > | ihost_config.daemon=1; | 
 
 
 
 
 | 654 | > | /* Set all errors to go down stderr until told otherwise */ | 
 
 
 
 
 | 655 | > | ihost_config.log=stderr; | 
 
 
 
 
 | 656 | > |  | 
 
 
 
 
 | 657 | > | /* Blank ihost_state to default settings */ | 
 
 
 
 
 | 658 | > | ihost_state.filtermanager_host=NULL; | 
 
 
 
 
 | 659 | > | ihost_state.host_fqdn=NULL; | 
 
 
 
 
 | 660 | > | ihost_state.host_ip=NULL; | 
 
 
 
 
 | 661 | > | ihost_state.server_fqdn=NULL; | 
 
 
 
 
 | 662 | > | ihost_state.file_list=NULL; | 
 
 
 
 
 | 663 | > | ihost_state.last_modified=NULL; | 
 
 
 
 
 | 664 | > |  | 
 
 
 
 
 | 665 | > | while((cmdopt=getopt(argc, argv, "vfhV")) != -1){ | 
 
 
 
 
 | 666 | > | switch(cmdopt){ | 
 
 
 
 
 | 667 | > | case 'v': | 
 
 
 
 
 | 668 | > | ihost_config.verbose++; | 
 
 
 
 
 | 669 | > | break; | 
 
 
 
 
 | 670 | > |  | 
 
 
 
 
 | 671 | > | case 'f': | 
 
 
 
 
 | 672 | > | /* Force syslog logging since stderr will be closed in this case */ | 
 
 
 
 
 | 673 | > | ihost_config.daemon=0; | 
 
 
 
 
 | 674 | > | break; | 
 
 
 
 
 | 675 | > |  | 
 
 
 
 
 | 676 | > | case 'h': | 
 
 
 
 
 | 677 | > | usage(argv[0]); | 
 
 
 
 
 | 678 | > | break; | 
 
 
 
 
 | 679 | > |  | 
 
 
 
 
 | 680 | > | case 'V': | 
 
 
 
 
 | 681 | > | fprintf(stderr, "%s version %s\n", argv[0], VERSION); | 
 
 
 
 
 | 682 | > | break; | 
 
 
 
 
 | 683 | > |  | 
 
 
 
 
 | 684 | > | default: | 
 
 
 
 
 | 685 | > | usage(argv[0]); | 
 
 
 
 
 | 686 | > | exit(1); | 
 
 
 
 
 | 687 | > | } | 
 
 
 
 
 | 688 | > | } | 
 
 
 
 
 | 689 | > |  | 
 
 
 
 
 | 690 | > | if(argc!=optind+2){ | 
 
 
 
 
 | 691 | > | usage(argv[0]); | 
 
 
 
 
 | 692 | > | exit(1); | 
 
 
 
 
 | 693 | > | } | 
 
 
 
 
 | 694 | > |  | 
 
 
 
 
 | 695 | > | ihost_state.filtermanager_host=strdup(argv[optind]); | 
 
 
 
 
 | 696 | > | ihost_state.filtermanager_port=atoi(argv[optind+1]); | 
 
 
 
 
 | 697 | > |  | 
 
 
 
 
 | 698 | > | if(gethostbyname(ihost_state.filtermanager_host)==NULL){ | 
 
 
 
 
 | 699 | > | log_msg(LOG_CRIT, "Failed to lookup hostname. Please check settings"); | 
 
 
 
 
 
 
 
 
 
 
 | 700 |  | exit(1); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 701 |  | } | 
 
 
 
 
 
 
 
 | 702 | + | if(ihost_state.filtermanager_port==0){ | 
 
 
 
 
 
 
 
 | 703 | + | log_msg(LOG_ERR, "Invalid port number"); | 
 
 
 
 
 
 
 
 | 704 | + | exit(1); | 
 
 
 
 
 
 
 
 | 705 | + | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 706 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 707 | < | ihost_state.fm_host=argv[1]; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 708 | < | ihost_state.fm_port=atoi(argv[2]); | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 707 | > | if(ihost_config.daemon){ | 
 
 
 
 
 | 708 | > | pid=fork(); | 
 
 
 
 
 | 709 | > | if(pid==-1){ | 
 
 
 
 
 | 710 | > | log_msg(LOG_CRIT, "Failed to background exiting"); | 
 
 
 
 
 | 711 | > | exit(1); | 
 
 
 
 
 | 712 | > | }else if(pid!=0){ | 
 
 
 
 
 | 713 | > | /* Parent process */ | 
 
 
 
 
 | 714 | > | return 0; | 
 
 
 
 
 | 715 | > | } | 
 
 
 
 
 | 716 | > | /* We should now be in the background*/ | 
 
 
 
 
 | 717 | > | if(setsid()==-1){ | 
 
 
 
 
 | 718 | > | log_msg(LOG_CRIT, "setsid failed"); | 
 
 
 
 
 | 719 | > | exit(1); | 
 
 
 
 
 | 720 | > | } | 
 
 
 
 
 | 721 | > |  | 
 
 
 
 
 | 722 | > | if((ihost_config.log=fopen(LOG_FILE, "a"))==NULL){ | 
 
 
 
 
 | 723 | > | ihost_config.log=stderr; | 
 
 
 
 
 | 724 | > | log_msg(LOG_CRIT, "Failed to open Logfiles %s for writing", LOG_FILE); | 
 
 
 
 
 | 725 | > | exit(1); | 
 
 
 
 
 | 726 | > | } | 
 
 
 
 
 
 
 
 
 
 
 | 727 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 728 | < | if(ihost_configure(&ihost_state)!=0){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 729 | < | errf("configure failed"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 730 | < | /* Ok, ideally we prob should have 2 copies of the structure and carry on if this | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 731 | < | happens.. But we dont :) (at the moment) */ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 732 | < | exit(1); | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 728 | > | fclose(stdin); | 
 
 
 
 
 | 729 | > | fclose(stdout); | 
 
 
 
 
 | 730 | > | fclose(stderr); | 
 
 
 
 
 | 731 | > |  | 
 
 
 
 
 | 732 | > | } | 
 
 
 
 
 | 733 | > |  | 
 
 
 
 
 | 734 | > | log_msg(LOG_INFO, "Starting ihost"); | 
 
 
 
 
 | 735 | > |  | 
 
 
 
 
 | 736 | > | log_msg(LOG_DEBUG,"Writing PID FILE"); | 
 
 
 
 
 | 737 | > |  | 
 
 
 
 
 | 738 | > | pid=getpid(); | 
 
 
 
 
 | 739 | > |  | 
 
 
 
 
 | 740 | > | if((f=fopen(PID_FILE,"w")) == NULL){ | 
 
 
 
 
 | 741 | > | log_msg(LOG_CRIT, "Failed to write PID file"); | 
 
 
 
 
 | 742 | > | }else{ | 
 
 
 
 
 | 743 | > | if((fprintf(f,"%d",(int)pid)) <= 0 ){ | 
 
 
 
 
 | 744 | > | log_msg(LOG_CRIT, "Failed to write PID file"); | 
 
 
 
 
 | 745 | > | } | 
 
 
 
 
 | 746 | > | if((fclose(f))!=0){ | 
 
 
 
 
 | 747 | > | log_msg(LOG_CRIT, "failed to close PID file"); | 
 
 
 
 
 | 748 | > | } | 
 
 
 
 
 | 749 | > | } | 
 
 
 
 
 | 750 | > |  | 
 
 
 
 
 | 751 | > | /* Get the initial config from the filter manager. Should this fail, | 
 
 
 
 
 | 752 | > | * wait, and then try again. */ | 
 
 
 
 
 | 753 | > |  | 
 
 
 
 
 | 754 | > | get_diskio_stats_diff(&packet_num); | 
 
 
 
 
 | 755 | > | packet_num=0; | 
 
 
 
 
 | 756 | > |  | 
 
 
 
 
 | 757 | > | while(ihost_getconfig(&ihost_state)!=0){ | 
 
 
 
 
 | 758 | > | log_msg(LOG_ERR, "Failed to get ihost config"); | 
 
 
 
 
 | 759 | > | sleep(10); | 
 
 
 
 
 
 
 
 
 
 
 | 760 |  | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 761 |  |  | 
 
 
 
 
 
 
 
 | 762 | + | printf("%s\n%d\n", ihost_state.server_fqdn, ihost_state.server_udp_port); | 
 
 
 
 
 
 
 
 | 763 | + | while((create_udp_sockinfo(&udp_sockinfo, ihost_state.server_fqdn, ihost_state.server_udp_port))!=0){ | 
 
 
 
 
 
 
 
 | 764 | + | log_msg(LOG_ERR, "Failed to create udp socket"); | 
 
 
 
 
 
 
 
 | 765 | + | sleep(10); | 
 
 
 
 
 
 
 
 | 766 | + | } | 
 
 
 
 
 
 
 
 | 767 | + |  | 
 
 
 
 
 
 
 
 | 768 | + | config_time=time(NULL)+ihost_state.config_ttl; | 
 
 
 
 
 
 
 
 | 769 | + |  | 
 
 
 
 
 
 
 
 | 770 | + | /* Now have config.. collect data and send as often as required */ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 771 |  | for(;;){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 772 |  | cur_time=time(NULL); | 
 
 
 
 
 
 
 
 
 | 428 | – | if(cur_time>=tcp_time){ | 
 
 
 
 
 
 
 
 
 | 429 | – | /*printf("sending TCP\n");*/ | 
 
 
 
 
 
 
 
 
 | 430 | – | heartbeat_exit=heartbeat(&ihost_state); | 
 
 
 
 
 
 
 
 
 | 431 | – | if(heartbeat_exit==RECONFIGURE_RETURN_CODE){ | 
 
 
 
 
 
 
 
 
 | 432 | – | /*errf("heartbeat needs to be reconfigured");*/ | 
 
 
 
 
 
 
 
 
 | 433 | – | ihost_configure(&ihost_state); | 
 
 
 
 
 
 
 
 
 | 434 | – | /* So udp doesn't wait til next sending before updating */ | 
 
 
 
 
 
 
 
 
 | 435 | – | udp_time=0; | 
 
 
 
 
 
 
 
 
 | 436 | – | } | 
 
 
 
 
 
 
 
 
 | 437 | – | if(heartbeat_exit==-1){ | 
 
 
 
 
 
 
 
 
 | 438 | – | errf("ah crap"); | 
 
 
 
 
 
 
 
 
 | 439 | – | exit(1); | 
 
 
 
 
 
 
 
 
 | 440 | – | } | 
 
 
 
 
 
 
 
 
 | 441 | – | tcp_time=time(NULL)+ihost_state.tcp_update_time; | 
 
 
 
 
 
 
 
 
 | 442 | – | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 773 |  |  | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 774 |  | if(cur_time>=udp_time){ | 
 
 
 
 
 
 
 
 
 
 
 | 775 | < | /*printf("sending UDP\n");*/ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 776 | < | stat_grab_time=time(NULL); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 447 | < | if((xml_stats=stat_grab(&ihost_state, counter++)) == NULL){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 448 | < | errf("Failed to get stats (%m)"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 449 | < | exit(1); | 
 
 
 
 
 
 
 
 
 | 775 | > | if((get_system_stats(packet_num++, &ihost_state, packet, MAX_UDP_PACKET_SIZE))!=0){ | 
 
 
 
 
 | 776 | > | log_msg(LOG_ERR, "Failed to get system stats"); | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 777 |  | } | 
 
 
 
 
 
 
 
 
 
 
 | 778 | < | stat_grab_time=time(NULL)-stat_grab_time; | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 779 | < | send_stats(&ihost_state, xml_stats); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 780 | < | free(xml_stats); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 781 | < | udp_time=time(NULL)+ihost_state.udp_update_time-stat_grab_time; | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 778 | > |  | 
 
 
 
 
 | 779 | > | len=strlen(packet); | 
 
 
 
 
 | 780 | > | log_msg(LOG_DEBUG, "Packet size: %d\nPacket: %s\n", len, packet); | 
 
 
 
 
 | 781 | > |  | 
 
 
 
 
 | 782 | > | if((sendto(udp_sockinfo.sock, packet, len, 0, (struct sockaddr *) &udp_sockinfo.addr, sizeof(udp_sockinfo.addr)))!=len){ | 
 
 
 
 
 | 783 | > | log_msg(LOG_CRIT, "Failed to send packet"); | 
 
 
 
 
 | 784 | > | } | 
 
 
 
 
 | 785 | > | udp_time=cur_time+ihost_state.udp_update_time; | 
 
 
 
 
 | 786 | > | log_msg(LOG_DEBUG, "Next packet should be sent on %d", udp_time); | 
 
 
 
 
 
 
 
 
 
 
 | 787 |  | } | 
 
 
 
 
 
 
 
 | 788 | + |  | 
 
 
 
 
 
 
 
 | 789 | + | if(cur_time>=config_time){ | 
 
 
 
 
 
 
 
 | 790 | + | if(ihost_getconfig(&ihost_state)!=0){ | 
 
 
 
 
 
 
 
 | 791 | + | /* If we can't get the config, try again 5 minutes time */ | 
 
 
 
 
 
 
 
 | 792 | + | log_msg(LOG_ERR, "Failed to get config, try again 5 minutes time"); | 
 
 
 
 
 
 
 
 | 793 | + | config_time=time(NULL)+300; | 
 
 
 
 
 
 
 
 | 794 | + | }else{ | 
 
 
 
 
 
 
 
 | 795 | + | close(udp_sockinfo.sock); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 796 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 797 | < | if(tcp_time<udp_time){ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 798 | < | sleep_delay=tcp_time-time(NULL); | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 799 | < | }else{ | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 800 | < | sleep_delay=udp_time-time(NULL); | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 797 | > | while((create_udp_sockinfo(&udp_sockinfo, ihost_state.server_fqdn, ihost_state.server_udp_port))!=0){ | 
 
 
 
 
 | 798 | > | log_msg(LOG_CRIT, "Failed to create udp socket"); | 
 
 
 
 
 | 799 | > | sleep(10); | 
 
 
 
 
 | 800 | > | } | 
 
 
 
 
 | 801 | > |  | 
 
 
 
 
 | 802 | > | config_time=time(NULL)+ihost_state.config_ttl; | 
 
 
 
 
 | 803 | > |  | 
 
 
 
 
 | 804 | > | log_msg(LOG_DEBUG, "Config expires on %d\n", ihost_state.config_ttl); | 
 
 
 
 
 | 805 | > | } | 
 
 
 
 
 
 
 
 
 
 
 | 806 |  | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 807 |  |  | 
 
 
 
 
 
 
 
 
 
 
 | 808 | < | /*printf("tcp epoc: %ld \t udp epoc: %ld\ntime:%ld \tsleeping: %d\n", tcp_time, udp_time, time(NULL), sleep_delay);*/ | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 808 | > | sleep_delay=udp_time-time(NULL); | 
 
 
 
 
 | 809 | > | log_msg(LOG_DEBUG, "Sleeping for %d", sleep_delay); | 
 
 
 
 
 
 
 
 
 
 
 | 810 |  | if(sleep_delay>0) sleep(sleep_delay); | 
 
 
 
 
 
 
 
 
 
 
 | 811 | < | } | 
 
 
 
 
 
 
 
 
 
 
 
 
 | 812 | < | return 0; | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 811 | > | } | 
 
 
 
 
 | 812 | > |  | 
 
 
 
 
 | 813 | > | return(0); | 
 
 
 
 
 
 
 
 
 
 
 | 814 |  | } | 
 
 
 
 
 
 
 
 
 | 468 | – |  |