]
トップ «前の日記(2011-11-04) 最新 次の日記(2011-12-15)» 編集

Yukiharu YABUKI の tDiary



このtDiaryを検索します。

2011-12-10 [長年日記]

_ nagios-plugin-nrpe の設定のキモとなりそうないくつかの点

nagiosという監視システムがある。わりと老舗の監視システムで、設定を覚えるまでは、ちょいと苦労するかもしれないが、覚えてしまえば、かなり柔軟なことができるシステムである。

もちろん、Debian GNU/Linux にも収録されており、デフォルトでインストールすると、自分自身を監視するデフォルトの設定ができあがっており、そこから設定例をみて自分で拡張することができる。

今回記事を書こうと思ったのは、Google検索上位で NRPE の設定について、いくつか参照をさせていただいた。その中で読者をミスリーディングさせるような記事もあり、NRPEの設定方法について、いくつかのチェックポイントを書いておきたい。

本来は、NagiosとNRPEの関係など詳しく書くべきだか、リモートホストの資源管理nrpeのインストール(http://anabuki.dip.jp/linkstation/nrpe.html)の図を参照していただきたい。

  1. どのサービスからNRPEが起動されるのか確認しよう。

    私の触っている範囲の、CentOSとDebianでは、DaemonでNRPEを上げているパターンなのだが、世の中には、xinetdで、NRPEを上げる作法もあるようだ。xinetd経由だと、 /etc/nagios/nrpe.cfg の allowed_hosts を参照しないようなのである。だから、allowd_hosts=xxx.xxx.xxx.xxx/24という表記をしているが、xinetd 経由だと一見、動いているように見える。

    だが、それだとdaemonに切り替えた時に動かなくて、「相性」の問題にされている節がある。そこで、提案したいのが、ps auxw コマンドなどで、nrpe が起動している時のオプションを確認すること。manなどを確認するとわかるが、-d が daemon モード -i が inetd または xinetd で起動されている。これをみることで allowed_hosts が有効かどうかを確認するのである。

  2. allowed_hostsの書き方

    上記でも書いたが、allowed_hostsには、xxx.xxx.xxx.xxx/24という表記は使えない。ではどのような表記ができるのか。

    forkで子プロセスが生成されて、シグナルを受け付けるなどの一般的な処理があって...
       871                                  /* is this is a blessed machine? */
       872                                  if(allowed_hosts){
       873  
       874                                          if(!is_an_allowed_host(inet_ntoa(nptr->sin_addr))){
       875  
       876                                                 /* log error to syslog facility */
       877                                                 syslog(LOG_ERR,"Host %s is not allowed to talk to us!",inet_ntoa(nptr->sin_addr));
       878  
       879                                                 /* log info to syslog facility */
       880                                                 if(debug==TRUE)
       881                                                         syslog(LOG_DEBUG,"Connection from %s closed.",inet_ntoa(nptr->sin_addr));
       882  
       883                                                 /* close socket prior to exiting */
       884                                                 close(new_sd);
       885  
       886                                                 exit(STATE_OK);
       887                                                 }
       888                                         else{
       889  
       890                                                 /* log info to syslog facility */
       891                                                 if(debug==TRUE)
       892                                                         syslog(LOG_DEBUG,"Host address is in allowed_hosts");
       893                                                 }
       894                                          }
       896  #ifdef HAVE_LIBWRAP
       897  
       898                                  /* Check whether or not connections are allowed from this host */
       899                                  request_init(&req,RQ_DAEMON,"nrpe",RQ_FILE,new_sd,0);
       900                                  fromhost(&req);
       901  
       902                                  if(!hosts_access(&req)){
       903  
       904                                          syslog(LOG_DEBUG,"Connection refused by TCP wrapper");
       905  
       906                                          /* refuse the connection */
       907                                          refuse(&req);
       908                                          close(new_sd);
       909  
       910                                          /* should not be reached */
       911                                          syslog(LOG_ERR,"libwrap refuse() returns!");
       912                                          exit(STATE_CRITICAL);
       913                                          }
       914  #endif
       915  
       916                                  /* handle the client connection */
       917                                  handle_connection(new_sd);
       918  
       919                                  /* log info to syslog facility */
       920                                  if(debug==TRUE)
       921                                          syslog(LOG_DEBUG,"Connection from %s closed.",inet_ntoa(nptr->sin_addr));
       922  
       923                                  /* close socket prior to exiting */
       924                                  close(new_sd);
       925  
       926                                  exit(STATE_OK);
       927                                  }
       928  
       929                          /* first child returns immediately, grandchild is inherited by INIT process -> no zombies.
    .. */
       930                          else
       931                                  exit(STATE_OK);
       932                          }
    
       952  /* checks to see if a given host is allowed to talk to us */
       953  int is_an_allowed_host(char *connecting_host){
       954          char *temp_buffer=NULL;
       955          char *temp_ptr=NULL;
       956          int result=0;
       957          struct hostent *myhost;
       958          char **pptr=NULL;
       959          char *save_connecting_host=NULL;
       960          struct in_addr addr;
       961          
       962          /* make sure we have something */
       963          if(connecting_host==NULL)
       964                  return 0;
       965          if(allowed_hosts==NULL)
       966                  return 1;
       967  
       968          if((temp_buffer=strdup(allowed_hosts))==NULL)
       969                  return 0;
       970          
       971          /* try and match IP addresses first */
       972          for(temp_ptr=strtok(temp_buffer,",");temp_ptr!=NULL;temp_ptr=strtok(NULL,",")){
       973  
       974                  if(!strcmp(connecting_host,temp_ptr)){
       975                          result=1;
       976                          break;
       977                          }
       978                  }
       979  
       980          /* try DNS lookups if needed */
       981          if(result==0){
       982  
       983                  free(temp_buffer);
       984                  if((temp_buffer=strdup(allowed_hosts))==NULL)
       985                          return 0;
       986  
       987                  save_connecting_host=strdup(connecting_host);
       988                  for(temp_ptr=strtok(temp_buffer,",");temp_ptr!=NULL;temp_ptr=strtok(NULL,",")){
       989  
       990                          myhost=gethostbyname(temp_ptr);
       991                          if(myhost!=NULL){
       992  
       993                                  /* check all addresses for the host... */
       994                                  for(pptr=myhost->h_addr_list;*pptr!=NULL;pptr++){
       995                                          memcpy(&addr, *pptr, sizeof(addr));
       996                                          if(!strcmp(save_connecting_host, inet_ntoa(addr))){
       997                                                  result=1;
       998                                                  break;
       999                                                  }
      1000                                          }
      1001                                  }
      1002  
      1003                          if(result==1)
      1004                                  break;
      1005                          }
      1006  
      1007                  strcpy(connecting_host, save_connecting_host);
      1008                  free(save_connecting_host);
      1009                  }
      1010  
      1011          free(temp_buffer);
      1012  
      1013          return result;
      1014          }
    
    ソースコードnrpe.c 2.12の該当する部分である。libwrapのこと、allwed_hostsのチェック方法などがわかる。上記のチェックさえ通れば、FQDNでもいいわけである。

    apt-get source でソースを取ってきて、すぐに調べるといいのではないだろうか。
  3. あと、NRPEがきちんと設定できれば、CentOSだろうがDebianだろうが、相互に監視できる。
以上、簡単にソースコードを調べるだけで、具体的な設定の書き方がわかるので、apt-get source や、Redhat系でも、それに準じるコマンドで、ソースを確認していただきたい。