ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/ihost/ihost.c
(Generate patch)

Comparing projects/cms/source/ihost/ihost.c (file contents):
Revision 1.33 by pajs, Sat Mar 8 20:41:46 2003 UTC vs.
Revision 1.44 by pajs, Mon Jan 5 15:22:17 2004 UTC

# Line 1 | Line 1
1   /*
2   * i-scream central monitoring system
3 < * http://www.i-scream.org.uk
3 > * http://www.i-scream.org
4   * Copyright (C) 2000-2002 i-scream
5   *
6   * This program is free software; you can redistribute it and/or
# Line 31 | Line 31
31   #include <stdarg.h>
32   #include <errno.h>
33   #include <netdb.h>
34 + #include <netinet/in.h>
35  
36 < #include "ukcprog.h"
37 < #include "statgrab.h"
36 > #include <ukcprog.h>
37 > #include <statgrab.h>
38  
39   #define LOG_CRIT 0
40   #define LOG_ERR 1
# Line 46 | Line 47 | typedef struct{
47  
48          char *host_ip;
49          char *host_fqdn;
50 +        int preset_fqdn;
51 +        int preset_ip;
52  
53          char *server_fqdn;
54          int server_udp_port;
# Line 77 | Line 80 | ihost_config_t ihost_config;
80  
81   extern int errno;
82  
83 < /* Taken from the openssh code. Its licence included in function.*/
84 < //#ifndef HAVE_STRLCAT
83 > /* Taken from the OpenSSH code. Its licence included in function.*/
84 > #ifndef HAVE_STRLCAT
85  
86   /*
87   * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
# Line 106 | Line 109 | extern int errno;
109   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
110   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
111   */
112 +
113   /*
114   * Appends src to string dst of size siz (unlike strncat, siz is the
115   * full size of dst, not space left).  At most siz-1 characters
# Line 144 | Line 148 | strlcat(dst, src, siz)
148          return(dlen + (s - src));       /* count does not include NUL */
149   }
150  
151 < //#endif
151 > #endif
152 > /* End strlcat function taken from OpenSSH */
153  
149
154   void log_msg(int level, char *format, ...){
155          int cur_errno;
156          va_list ap;
# Line 162 | Line 166 | void log_msg(int level, char *format, ...){
166                  }else{
167                          fprintf(ihost_config.log, "\n");
168                  }
169 +                fflush(ihost_config.log);
170          }
171   }
172  
# Line 221 | Line 226 | FILE *create_tcp_connection(char *hostname, int port){
226  
227          if((get_host_addr(hostname, &haddr))!=0){
228                  log_msg(LOG_CRIT, "Failed to lookup name for %s", hostname);
229 +                close(sock);
230                  return NULL;
231          }
232  
# Line 232 | Line 238 | FILE *create_tcp_connection(char *hostname, int port){
238          log_msg(LOG_DEBUG, "Creating a tcp connection");
239          if(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) !=0){
240                  log_msg(LOG_CRIT, "Failed to connect to hostname %s on port %d", hostname, port);
241 +                close(sock);
242                  return NULL;
243          }
244  
# Line 292 | Line 299 | int ihost_getconfig(ihost_state_t *ihost_state){
299          time_t config_ttl=0;
300  
301          if((tcp_con=create_tcp_connection(ihost_state->filtermanager_host, ihost_state->filtermanager_port))==NULL){
302 <                goto error;
302 >                return -1;
303          }
304  
305          if(ihost_state->file_list!=NULL || ihost_state->last_modified!=NULL){
# Line 380 | Line 387 | int ihost_getconfig(ihost_state_t *ihost_state){
387                          goto error;
388                  }
389  
390 <                printf("string : %s\n", response_ptr);
390 >                /*printf("string : %s\n", response_ptr);*/
391                  server_udp_port=atoi(response_ptr);
392  
393                  if (server_udp_port==0){
# Line 401 | Line 408 | int ihost_getconfig(ihost_state_t *ihost_state){
408           * this already be NULL */
409          if(ihost_state->file_list!=NULL) free(ihost_state->file_list);
410          if(ihost_state->last_modified!=NULL) free(ihost_state->last_modified);
404        if(ihost_state->host_fqdn!=NULL) free(ihost_state->host_fqdn);
411          if(ihost_state->server_fqdn!=NULL) free(ihost_state->server_fqdn);
406        if(ihost_state->host_ip!=NULL) free(ihost_state->host_ip);
412  
413 +        if(ihost_state->preset_fqdn){
414 +                if(host_fqdn != NULL) free(host_fqdn);
415 +        }else{
416 +                if(ihost_state->host_fqdn!=NULL) free(ihost_state->host_fqdn);
417 +                ihost_state->host_fqdn=host_fqdn;
418 +        }
419 +
420 +        if(ihost_state->preset_ip){
421 +                if(host_ip != NULL) free(host_ip);
422 +        }else{
423 +                if(ihost_state->host_ip!=NULL) free(ihost_state->host_ip);
424 +                ihost_state->host_ip=host_ip;
425 +        }
426 +
427 +
428          ihost_state->file_list=file_list;
429          ihost_state->last_modified=last_modified;
410        ihost_state->host_fqdn=host_fqdn;
411        ihost_state->host_ip=host_ip;
430          ihost_state->server_fqdn=server_fqdn;
431          ihost_state->server_udp_port=server_udp_port;
432          ihost_state->udp_update_time=udp_update_time;
# Line 577 | Line 595 | int get_system_stats(int seq_no, ihost_state_t *ihost_
595                          y=page_stats->pages_pageout;
596                  }
597                  snprintf(tmp, size, \
598 <                        "<pages><swapins>%lld</swapins><swapouts>%lld</swapouts></pages>", \
598 >                        "<pages><pageins>%lld</pageins><pageouts>%lld</pageouts></pages>", \
599                          x, \
600                          y);
601          
# Line 694 | Line 712 | too_big_error:
712  
713          
714   void usage(char *progname){
715 <        fprintf(stderr, "Usage %s [options] server port\n", progname);
716 <        fprintf(stderr, "Options\n");
717 <        fprintf(stderr, "  -v           Verbose mode,-vv would make even more verbose\n");
718 <        fprintf(stderr, "  -f           Foreground mode, print errors to stderr\n");
719 <        fprintf(stderr, "  -V           Print version number\n");
720 <        fprintf(stderr, "  -h           Prints this help page\n");
715 >        fprintf(stderr, "Usage %s [-v[v]] [-f] [-n name] [-i ip] [-s server] [-p port] [-V] [-h]\n\n", progname);
716 >        fprintf(stderr, "  -v    Verbose mode, -vv would make even more verbose\n");
717 >        fprintf(stderr, "  -f    Foreground mode, print errors to stderr\n");
718 >        fprintf(stderr, "  -n    Set the machine name to be reported as\n");
719 >        fprintf(stderr, "  -i    Set the IP to be reported as\n");
720 >        fprintf(stderr, "  -s    Specifies the i-scream server to connect to\n");
721 >        fprintf(stderr, "          default: %s\n", DEF_SERVER_NAME);
722 >        fprintf(stderr, "  -p    Specifies the i-scream server port\n");
723 >        fprintf(stderr, "          default: %d\n", DEF_SERVER_PORT);
724 >        fprintf(stderr, "  -V    Print version number\n");
725 >        fprintf(stderr, "  -h    Prints this help page\n");
726 >        fprintf(stderr, "\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT);
727          exit(1);
728   }
729  
# Line 726 | Line 750 | int main(int argc, char **argv){
750          ihost_config.log=stderr;
751  
752          /* Blank ihost_state to default settings */
753 <        ihost_state.filtermanager_host=NULL;
753 >        ihost_state.filtermanager_host=DEF_SERVER_NAME;
754 >        ihost_state.filtermanager_port=DEF_SERVER_PORT;
755          ihost_state.host_fqdn=NULL;
756          ihost_state.host_ip=NULL;
757 +        ihost_state.preset_fqdn = 0;
758 +        ihost_state.preset_ip = 0;
759          ihost_state.server_fqdn=NULL;
760          ihost_state.file_list=NULL;
761          ihost_state.last_modified=NULL;
762  
763 <        while((cmdopt=getopt(argc, argv, "vfhV")) != -1){
763 >        while((cmdopt=getopt(argc, argv, "vfhVs:i:")) != -1){
764                  switch(cmdopt){
765                          case 'v':
766                                  ihost_config.verbose++;
# Line 744 | Line 771 | int main(int argc, char **argv){
771                                  ihost_config.daemon=0;
772                                  break;
773  
747                        case 'h':
748                                usage(argv[0]);
749                                break;
750
774                          case 'V':
775                                  fprintf(stderr, "%s version %s\n", argv[0], VERSION);
776                                  break;
777 +                        case 'n':
778 +                                ihost_state.preset_fqdn = 1;
779 +                                ihost_state.host_fqdn = strdup(optarg);
780 +                                if(ihost_state.host_fqdn == NULL){
781 +                                        fprintf(stderr, "Missing hostname\n");  
782 +                                        usage(argv[0]);
783 +                                }
784 +                                break;
785 +                        case 'i':
786 +                                /* Hmm.. Someone could set any string to be the IP, and it will let it */
787 +                                ihost_state.preset_ip = 1;
788 +                                ihost_state.host_ip = strdup(optarg);
789 +                                if(ihost_state.host_ip == NULL){
790 +                                        fprintf(stderr, "Missing ip\n");        
791 +                                        usage(argv[0]);
792 +                                }
793 +                                break;
794  
795 +                        case 's':
796 +                                ihost_state.filtermanager_host=strdup(optarg);
797 +                                break;
798 +
799 +                        case 'p':
800 +                                ihost_state.filtermanager_port=atoi(optarg);
801 +                                break;
802 +
803 +                        case 'h':
804                          default:
805                                  usage(argv[0]);
806                                  exit(1);
807                  }
808          }
809  
761        if(argc!=optind+2){
762                usage(argv[0]);
763                exit(1);
764        }
765
766        ihost_state.filtermanager_host=strdup(argv[optind]);
767        ihost_state.filtermanager_port=atoi(argv[optind+1]);
768        
810          if(gethostbyname(ihost_state.filtermanager_host)==NULL){
811                  log_msg(LOG_CRIT, "Failed to lookup hostname. Please check settings");
812                  exit(1);
# Line 802 | Line 843 | int main(int argc, char **argv){
843  
844          }
845  
846 <        log_msg(LOG_INFO, "Starting ihost");
846 >        log_msg(LOG_INFO, "Starting ihost...");
847 >
848 >        log_msg(LOG_DEBUG, "Running statgrab_init()");
849 >        if(!statgrab_init()){
850 >                log_msg(LOG_CRIT, "statgrab_init failed (%m)");
851 >                exit(1);
852 >        }
853          
854          log_msg(LOG_DEBUG,"Writing PID FILE");
855  
# Line 819 | Line 866 | int main(int argc, char **argv){
866                  }
867          }
868  
869 +
870          /* Get the initial config from the filter manager. Should this fail,
871           * wait, and then try again. */
872  
# Line 830 | Line 878 | int main(int argc, char **argv){
878                  sleep(10);
879          }
880  
881 <        printf("%s\n%d\n", ihost_state.server_fqdn, ihost_state.server_udp_port);
881 >        /*printf("%s\n%d\n", ihost_state.server_fqdn, ihost_state.server_udp_port);*/
882          while((create_udp_sockinfo(&udp_sockinfo, ihost_state.server_fqdn, ihost_state.server_udp_port))!=0){
883                  log_msg(LOG_ERR, "Failed to create udp socket");        
884                  sleep(10);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines