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.39 by tdb, Sat Mar 29 18:49:49 2003 UTC vs.
Revision 1.50 by tdb, Sun May 30 17:13:05 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>
# 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 167 | Line 170 | void log_msg(int level, char *format, ...){
170          }
171   }
172  
170 /* Takes many pointers, checks if they are NULL or not, and then free's them */
171 /* Deprciated - and i only wrote it today! :)
172 void m_free(int num_pointers, ...){
173        int x=0;
174        va_list ap;
175        void *p;
176
177        va_start(ap, num_pointers);
178        for(;x<num_pointers;x++){
179                p=va_arg(ap, void*);
180                if(p!=NULL){
181                        free(p);
182                }
183        }
184        va_end(ap);
185 }
186 */
187
173   int create_udp_sockinfo(udp_sockinfo_t *udp_sockinfo, char *hostname, int port){
174  
175          struct in_addr haddr;
# Line 256 | Line 241 | int tcp_comm(FILE *f, char *send, char **response, cha
241          *response=fpgetline(f);
242          fseek(f, 0, SEEK_CUR);
243  
244 <        if(*response!=NULL) log_msg(LOG_DEBUG, "Recieved %s", *response);
244 >        if(*response!=NULL) log_msg(LOG_DEBUG, "Received %s", *response);
245  
246          if( (*response==NULL) || (strcmp(*response, "ERROR")==0) ) return -1;
247          
# Line 384 | Line 369 | int ihost_getconfig(ihost_state_t *ihost_state){
369                          goto error;
370                  }
371  
387                printf("string : %s\n", response_ptr);
372                  server_udp_port=atoi(response_ptr);
373  
374                  if (server_udp_port==0){
# Line 405 | Line 389 | int ihost_getconfig(ihost_state_t *ihost_state){
389           * this already be NULL */
390          if(ihost_state->file_list!=NULL) free(ihost_state->file_list);
391          if(ihost_state->last_modified!=NULL) free(ihost_state->last_modified);
408        if(ihost_state->host_fqdn!=NULL) free(ihost_state->host_fqdn);
392          if(ihost_state->server_fqdn!=NULL) free(ihost_state->server_fqdn);
410        if(ihost_state->host_ip!=NULL) free(ihost_state->host_ip);
393  
394 +        if(ihost_state->preset_fqdn){
395 +                if(host_fqdn != NULL) free(host_fqdn);
396 +        }else{
397 +                if(ihost_state->host_fqdn!=NULL) free(ihost_state->host_fqdn);
398 +                ihost_state->host_fqdn=host_fqdn;
399 +        }
400 +
401 +        if(ihost_state->preset_ip){
402 +                if(host_ip != NULL) free(host_ip);
403 +        }else{
404 +                if(ihost_state->host_ip!=NULL) free(ihost_state->host_ip);
405 +                ihost_state->host_ip=host_ip;
406 +        }
407 +
408 +
409          ihost_state->file_list=file_list;
410          ihost_state->last_modified=last_modified;
414        ihost_state->host_fqdn=host_fqdn;
415        ihost_state->host_ip=host_ip;
411          ihost_state->server_fqdn=server_fqdn;
412          ihost_state->server_udp_port=server_udp_port;
413          ihost_state->udp_update_time=udp_update_time;
# Line 437 | Line 432 | error:
432  
433   int get_system_stats(int seq_no, ihost_state_t *ihost_state, char *xml, int size){
434          char tmp[size];
435 <        cpu_percent_t *cpu_percent;    
436 <        mem_stat_t *mem_stats;
437 <        load_stat_t *load_stats;
438 <        user_stat_t *user_stats;
439 <        swap_stat_t *swap_stats;
440 <        general_stat_t *general_stats;
441 <        disk_stat_t *disk_stats;
442 <        diskio_stat_t *diskio_stats;
443 <        process_stat_t *process_stats;
444 <        network_stat_t *network_stats;
445 <        page_stat_t *page_stats;
435 >        sg_cpu_percents *cpu_percent;  
436 >        sg_mem_stats *mem_stats;
437 >        sg_load_stats *load_stats;
438 >        sg_user_stats *user_stats;
439 >        sg_swap_stats *swap_stats;
440 >        sg_host_info *general_stats;
441 >        sg_fs_stats *disk_stats;
442 >        sg_disk_io_stats *diskio_stats;
443 >        sg_process_count *process_stats;
444 >        sg_network_io_stats *network_stats;
445 >        sg_page_stats *page_stats;
446          int disk_entries=0;
447          int diskio_entries=0;
448          int network_entries=0;
# Line 461 | Line 456 | int get_system_stats(int seq_no, ihost_state_t *ihost_
456                   seq_no, ihost_state->host_fqdn, time(NULL), ihost_state->host_ip);
457  
458          /* Get cpu stats, check it is correct, then fill in its entry for the xml */
459 <        if((cpu_percent=cpu_percent_usage())==NULL){
459 >        if((cpu_percent=sg_get_cpu_percents())==NULL){
460                  log_msg(LOG_CRIT, "Failed to get cpu statistics");
461          }else{
462                  snprintf(tmp, size, \
# Line 477 | Line 472 | int get_system_stats(int seq_no, ihost_state_t *ihost_
472  
473          
474          /*Get mem stats, and fill in xml */    
475 <        if((mem_stats=get_memory_stats())==NULL){
475 >        if((mem_stats=sg_get_mem_stats())==NULL){
476                  log_msg(LOG_CRIT, "Failed to get memory statistics");
477          }else{
478                  snprintf(tmp, size, \
# Line 492 | Line 487 | int get_system_stats(int seq_no, ihost_state_t *ihost_
487  
488  
489          /* Get load stats */    
490 <        if((load_stats=get_load_stats())==NULL){
490 >        if((load_stats=sg_get_load_stats())==NULL){
491                  log_msg(LOG_CRIT, "Failed to get load statistics");
492          }else{
493                  snprintf(tmp, size, \
# Line 506 | Line 501 | int get_system_stats(int seq_no, ihost_state_t *ihost_
501  
502          /* get user stats */
503          
504 <        if((user_stats=get_user_stats())==NULL){
504 >        if((user_stats=sg_get_user_stats())==NULL){
505                  log_msg(LOG_CRIT, "Failed to get user statistics");
506          }else{
507  
# Line 520 | Line 515 | int get_system_stats(int seq_no, ihost_state_t *ihost_
515  
516  
517          /* swap stats */
518 <        if((swap_stats=get_swap_stats())==NULL){
518 >        if((swap_stats=sg_get_swap_stats())==NULL){
519                  log_msg(LOG_CRIT, "Failed to get swap statistics");    
520          }else{
521                  snprintf(tmp, size, \
# Line 535 | Line 530 | int get_system_stats(int seq_no, ihost_state_t *ihost_
530  
531          /* general stats */
532          
533 <        if((general_stats=get_general_stats())==NULL){
533 >        if((general_stats=sg_get_host_info())==NULL){
534                  log_msg(LOG_CRIT, "Failed to get general statistics");
535          }else{
536                  snprintf(tmp, size, \
# Line 553 | Line 548 | int get_system_stats(int seq_no, ihost_state_t *ihost_
548  
549          
550          /* process stats */
551 <        if((process_stats=get_process_stats())==NULL){  
551 >        if((process_stats=sg_get_process_count())==NULL){      
552                  log_msg(LOG_CRIT, "Failed to get general statistics");
553          }else{
554                  snprintf(tmp, size, \
# Line 570 | Line 565 | int get_system_stats(int seq_no, ihost_state_t *ihost_
565  
566  
567          /* Get paging stats */
568 <        if((page_stats=get_page_stats_diff())==NULL){
568 >        if((page_stats=sg_get_page_stats_diff())==NULL){
569                  log_msg(LOG_CRIT, "Failed to get paging statistics");
570          }else{
571                  if(page_stats->systime!=0){
# Line 591 | Line 586 | int get_system_stats(int seq_no, ihost_state_t *ihost_
586  
587          /* get diskio stats */
588          
589 <        if((diskio_stats=get_diskio_stats_diff(&diskio_entries))==NULL){
589 >        if((diskio_stats=sg_get_disk_io_stats_diff(&diskio_entries))==NULL){
590                  log_msg(LOG_CRIT, "Failed to get diskio statistics");
591          }else{
592                  strlcat(xml, "<diskio>", size);
# Line 624 | Line 619 | int get_system_stats(int seq_no, ihost_state_t *ihost_
619          
620          /* get networks stats */
621          
622 <        if((network_stats=get_network_stats_diff(&network_entries))==NULL){
622 >        if((network_stats=sg_get_network_io_stats_diff(&network_entries))==NULL){
623                  log_msg(LOG_CRIT, "Failed to get network statistics");
624          }else{
625                  strlcat(xml, "<net>", size);
# Line 656 | Line 651 | int get_system_stats(int seq_no, ihost_state_t *ihost_
651  
652          /* get disk stats */
653          
654 <        if((disk_stats=get_disk_stats(&disk_entries))==NULL){
654 >        if((disk_stats=sg_get_fs_stats(&disk_entries))==NULL){
655                  log_msg(LOG_CRIT, "Failed to get disk statistics");
656          }else{
657                  strlcat(xml, "<disk>", size);
# Line 698 | Line 693 | too_big_error:
693  
694          
695   void usage(char *progname){
696 <        fprintf(stderr, "Usage %s [options] server port\n", progname);
697 <        fprintf(stderr, "Options\n");
698 <        fprintf(stderr, "  -v           Verbose mode,-vv would make even more verbose\n");
699 <        fprintf(stderr, "  -f           Foreground mode, print errors to stderr\n");
700 <        fprintf(stderr, "  -V           Print version number\n");
701 <        fprintf(stderr, "  -h           Prints this help page\n");
696 >        fprintf(stderr, "Usage %s [-v[v]] [-f] [-n name] [-i ip] [-s server] [-p port] [-V] [-h]\n\n", progname);
697 >        fprintf(stderr, "  -v    Verbose mode, -vv would make even more verbose\n");
698 >        fprintf(stderr, "  -f    Foreground mode, print errors to stderr\n");
699 >        fprintf(stderr, "  -n    Set the machine name to be reported as\n");
700 >        fprintf(stderr, "  -i    Set the IP to be reported as\n");
701 >        fprintf(stderr, "  -s    Specifies the i-scream server to connect to\n");
702 >        fprintf(stderr, "          default: %s\n", DEF_SERVER_NAME);
703 >        fprintf(stderr, "  -p    Specifies the i-scream server port\n");
704 >        fprintf(stderr, "          default: %d\n", DEF_SERVER_PORT);
705 >        fprintf(stderr, "  -V    Print version number\n");
706 >        fprintf(stderr, "  -h    Prints this help page\n");
707 >        fprintf(stderr, "\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT);
708          exit(1);
709   }
710  
# Line 730 | Line 731 | int main(int argc, char **argv){
731          ihost_config.log=stderr;
732  
733          /* Blank ihost_state to default settings */
734 <        ihost_state.filtermanager_host=NULL;
734 >        ihost_state.filtermanager_host=DEF_SERVER_NAME;
735 >        ihost_state.filtermanager_port=DEF_SERVER_PORT;
736          ihost_state.host_fqdn=NULL;
737          ihost_state.host_ip=NULL;
738 +        ihost_state.preset_fqdn = 0;
739 +        ihost_state.preset_ip = 0;
740          ihost_state.server_fqdn=NULL;
741          ihost_state.file_list=NULL;
742          ihost_state.last_modified=NULL;
743  
744 <        while((cmdopt=getopt(argc, argv, "vfhV")) != -1){
744 >        while((cmdopt=getopt(argc, argv, "vfVn:i:s:p:h")) != -1){
745                  switch(cmdopt){
746                          case 'v':
747                                  ihost_config.verbose++;
# Line 748 | Line 752 | int main(int argc, char **argv){
752                                  ihost_config.daemon=0;
753                                  break;
754  
751                        case 'h':
752                                usage(argv[0]);
753                                break;
754
755                          case 'V':
756                                  fprintf(stderr, "%s version %s\n", argv[0], VERSION);
757                                  break;
758 +                        case 'n':
759 +                                ihost_state.preset_fqdn = 1;
760 +                                ihost_state.host_fqdn = strdup(optarg);
761 +                                if(ihost_state.host_fqdn == NULL){
762 +                                        fprintf(stderr, "Missing hostname\n");  
763 +                                        usage(argv[0]);
764 +                                }
765 +                                break;
766 +                        case 'i':
767 +                                /* Hmm.. Someone could set any string to be the IP, and it will let it */
768 +                                ihost_state.preset_ip = 1;
769 +                                ihost_state.host_ip = strdup(optarg);
770 +                                if(ihost_state.host_ip == NULL){
771 +                                        fprintf(stderr, "Missing ip\n");        
772 +                                        usage(argv[0]);
773 +                                }
774 +                                break;
775  
776 +                        case 's':
777 +                                ihost_state.filtermanager_host=strdup(optarg);
778 +                                break;
779 +
780 +                        case 'p':
781 +                                ihost_state.filtermanager_port=atoi(optarg);
782 +                                break;
783 +
784 +                        case 'h':
785                          default:
786                                  usage(argv[0]);
787                                  exit(1);
788                  }
789          }
790  
765        if(argc!=optind+2){
766                usage(argv[0]);
767                exit(1);
768        }
769
770        ihost_state.filtermanager_host=strdup(argv[optind]);
771        ihost_state.filtermanager_port=atoi(argv[optind+1]);
772        
791          if(gethostbyname(ihost_state.filtermanager_host)==NULL){
792                  log_msg(LOG_CRIT, "Failed to lookup hostname. Please check settings");
793                  exit(1);
# Line 806 | Line 824 | int main(int argc, char **argv){
824  
825          }
826  
827 <        log_msg(LOG_INFO, "Starting ihost");
827 >        log_msg(LOG_INFO, "Starting ihost...");
828 >
829 >        log_msg(LOG_DEBUG, "Running statgrab_init()");
830 >        if(statgrab_init()){
831 >                log_msg(LOG_CRIT, "statgrab_init failed (%m)");
832 >                exit(1);
833 >        }
834          
835          log_msg(LOG_DEBUG,"Writing PID FILE");
836  
# Line 834 | Line 858 | int main(int argc, char **argv){
858                  sleep(10);
859          }
860  
837        printf("%s\n%d\n", ihost_state.server_fqdn, ihost_state.server_udp_port);
861          while((create_udp_sockinfo(&udp_sockinfo, ihost_state.server_fqdn, ihost_state.server_udp_port))!=0){
862                  log_msg(LOG_ERR, "Failed to create udp socket");        
863                  sleep(10);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines