2013-10-24 4 views
1

nrpe를 사용하여 불량 dhcp 서버를 원격으로 확인하기 위해 사용자 정의 perl nagios 스크립트를 구현하려고합니다. 중앙 서버에 나는 실행하면 내 디버깅 로그에서nrpe가 사용자 정의 perl 스크립트를 실행할 수 없습니다. 반환 코드 : 1, 출력 : NRPE : 출력을 읽을 수 없습니다.

/usr/local/nagios/libexec/check_nrpe -H 10.9.0.25 -c check_roguedhcp 

내가이보고 있어요 : 나는 (심지어 NRPE 사용자로) 스크립트를 실행하면 로컬

Host is asking for command 'check_roguedhcp' to be run... 
Running command: sudo /usr/lib64/nagios/plugins/check_roguedhcp.pl 
Command completed with return code 1 and output: 
Return Code: 1, Output: NRPE: Unable to read output 

을 내가 예상 출력을 얻을 .

command[check_roguedhcp]=sudo /usr/lib64/nagios/plugins/check_roguedhcp.pl 
command[check_dhcp]=sudo /usr/lib64/nagios/plugins/check_dhcp -v 
nrpe_user=nrpe 
nrpe_group=nagios 

PS의 보조가 NRPE가

nrpe  5941 0.0 0.1 52804 2384 ?  Ss 08:25 0:00 /usr/sbin/nrpe -c /etc/nagios/nrpe.cfg -d 
(NRPE 그룹의 Nagios에) 사용자 NRPE로 실행 보여줍니다 내 /etc/nagios/nrpe.cfg는 다음과 같은 설정이 로컬 서버에서

나는 내가 가지고는 NRPE 호출을 수행 내 중앙 서버의/etc /의 sudoers

%nagios ALL=(ALL) NOPASSWD: /usr/lib/nagios64/plugins/check_dhcp, /usr/lib64/nagios/plugins/check_roguedhcp.pl 

을에 명령을 추가 한 다음과 같은 서비스 그룹 및 구성 :

define servicegroup{ 
     servicegroup_name  rogue_dhcp 
     alias     All dhcp monitors 
} 

define service{ 
     name       security-service 
     servicegroups     rogue_dhcp 
     register      0 
     max_check_attempts    1 
} 

Nagios는이 서버에 NRPE를 통해 등 스크립트 다른 check_users를 실행할 수 있습니다.

우리는 파일이 로컬에서 잘 실행된다는 것을 알고 있지만 펄 스크립트 자체는 다음과 같습니다.

1 #!/usr/bin/perl -w 
    2 # nagios: -epn 
    3 # the above makes nagios run the script separately. 
    4 use POSIX; 
    5 use lib "/usr/lib64/nagios/plugins"; 
    6 use utils qw(%ERRORS); 
    7 
    8 sub fail_usage { 
    9 if (scalar @_) { 
10  print "$0: error: \n"; 
11  map { print " $_\n"; } @_; 
12 } 
13 print "$0: Usage: \n"; 
14 print "$0 [-v [-v [-v]]] [ []] \n"; 
15 print "$0 [-v [-v [-v]]] [-s] [[-s] [[-s] ]] \n"; 
16 print " \n"; 
17 exit 3 ; 
18 } 
19 
20 my $verbose = 0; 
21 my %servers=( 
22     "x",  "10.x.x.x", 
23     "x",  "10.x.x.x", 
24     "x",  "10.x.x.x", 
25     "x",  "10.x.x.x" 
26); 
27 
28 # examine commandline args 
29 while ($ARGV=$ARGV[0]) { 
30 my $myarg = $ARGV; 
31 if ($ARGV eq '-s') { 
32  shift @ARGV; 
33  if (!($ARGV = $ARGV[0])) { fail_usage ("$myarg needs an argument"); } 
34  if ($ARGV =~ /^-/) { fail_usage ("$myarg must be followed by an argument"); } 
35  if (!defined($servers{$ARGV})) { $servers{$ARGV}=1; } 
36 } 
37 elsif ($ARGV eq '-v') { $verbose++; } 
38 elsif ($ARGV eq '-h' or $ARGV eq '--help') { fail_usage ; } 
39 elsif ($ARGV =~ /^-/) { fail_usage " invalid option ($ARGV)"; } 
40 elsif ($ARGV =~ /^\d+\.\d+\.\d+\.\d+$/) 
41 # servers should be ip addresses. I'm not doing detailed checks for this. 
42  { if (!defined($servers{$ARGV})) { $servers{$ARGV}=1; } } 
43 else { last; } 
44 shift @ARGV; 
45 } 
46 # for some reason I can't test for empty ARGs in the while loop 
47 @ARGV = grep {!/^\s*$/} @ARGV; 
48 if (scalar @ARGV) { fail_usage "didn't understand arguments: (".join (" ",@ARGV).")"; } 
49 
50 my $serversn = scalar keys %servers; 
51 
52 if ($verbose > 2) { 
53 print "verbosity=($verbose)\n"; 
54 print "servers = ($serversn)\n"; 
55 if ($serversn) { for my $i (keys %servers) { print "server ($i)\n"; } } 
56 } 
57 
58 if (!$serversn) { fail_usage "no servers"; } 
59 my $responses=0; 
60 my $responders=""; 
61 my @check_dhcp = qx{/usr/lib64/nagios/plugins/check_dhcp -v}; 
62 foreach my $value (@check_dhcp) { 
63 if ($value =~ /Added offer from server \@ /i){ 
64  $value =~ m/(\d+\.\d+\.\d+\.\d+)/i; 
65  my $host = $1; 
66  # we find a server in our list 
67  if (defined($servers{$host})) { $responses++; $responders.="$host "; } 
68  # we find a rogue DHCP server. Danger Will Robinson! 
69  else { 
70  print "DHCP:CRITICAL: DHCP service running on $host"; 
71  exit $ERRORS{'OK'} 
72  } 
73 } 
74 } 
75 # we saw all the servers in our list. All is good. 
76 if ($responses == $serversn) { 
77 print "DHCP:OK: $responses of $serversn Expected Responses to DHCP Broadcast"; 
78 exit $ERRORS{'OK'}; 
79 } 
80 # we found no DHCP responses. 
81 if ($responses == 0) { 
82 print "DHCP:OK: no rogue servers detected!!!!#[email protected]#"; 
83 exit $ERRORS{'OK'} 
84 } 
85 # we found less DHCP servers than we should have. Oh Nos! 
86 $responders =~ s/ $//; 
87 print "DHCP:OK: $responses of $serversn Responses to DHCP Broadcast. ($responders) responded. "; 
88 exit $ERRORS{'OK'}; 

다음은 nrpe 프로세스의 strace를 수행했을 때의 (관련성) 내용입니다.

955 6950 stat("/usr/lib64/nagios/plugins/check_roguedhcp.pl", {st_mode=S_IFREG|S_ISUID|S_ISGID|0755, st_size=2799, ...}) = 0 
956 6950 setresuid(4294967295, 4294967295, 4294967295) = 0 
957 6950 setresgid(4294967295, 536347864, 4294967295) = 0 
958 6950 setgroups(3, [536347864, 536347137, 536353632]) = 0 
959 6950 open("/dev/tty", O_RDWR|O_NOCTTY) = -1 ENXIO (No such device or address) 
960 6950 socket(PF_NETLINK, SOCK_RAW, 9) = 3 
961 6950 fcntl(3, F_SETFD, FD_CLOEXEC)  = 0 
962 6950 fcntl(3, F_SETFD, FD_CLOEXEC)  = 0 
963 6950 ioctl(0, SNDCTL_TMR_TIMEBASE or TCGETS, 0x7fff3de81ac0) = -1 ENOTTY (Inappropriate ioctl for device) 
964 6950 ioctl(1, SNDCTL_TMR_TIMEBASE or TCGETS, 0x7fff3de81ac0) = -1 EINVAL (Invalid argument) 
965 6950 ioctl(2, SNDCTL_TMR_TIMEBASE or TCGETS, 0x7fff3de81ac0) = -1 ENOTTY (Inappropriate ioctl for device) 
966 6950 getcwd("/", 4096)     = 2 
967 6950 sendto(3, "d\0\0\0c\4\5\0\1\0\0\0\0\0\0\0cwd=\"/\" cmd=\"/us"..., 100, 0, {sa_family=AF_NETLINK, pid=0, groups=00000000}, 12)  = 100 
968 6950 poll([{fd=3, events=POLLIN}], 1, 500) = 1 ([{fd=3, revents=POLLIN}]) 
969 6950 recvfrom(3, "$\0\0\0\2\0\0\0\1\0\0\0&\33\0\0\0\0\0\0d\0\0\0c\4\5\0\1\0\0\0"..., 8988, MSG_PEEK|MSG_DONTWAIT, {sa_family=AF_NE  TLINK, pid=0, groups=00000000}, [12]) = 36 
970 6950 recvfrom(3, "$\0\0\0\2\0\0\0\1\0\0\0&\33\0\0\0\0\0\0d\0\0\0c\4\5\0\1\0\0\0"..., 8988, MSG_DONTWAIT, {sa_family=AF_NETLINK, pi  d=0, groups=00000000}, [12]) = 36 
971 6950 write(2, "sudo", 4)    = 4 
972 6950 write(2, ": ", 2)     = 2 
973 6950 write(2, "sorry, you must have a tty to ru"..., 38) = 38 
974 6950 write(2, "\n", 1)     = 1 
975 6950 setresuid(4294967295, 4294967295, 4294967295) = 0 
976 6950 setresgid(4294967295, 4294967295, 4294967295) = 0 
977 6950 exit_group(1)      = ? 
978 6949 <... read resumed> "", 4096)  = 0 
979 6949 --- SIGCHLD (Child exited) @ 0 (0) --- 
980 6949 close(5)       = 0 
981 6949 wait4(6950, [{WIFEXITED(s) && WEXITSTATUS(s) == 1}], 0, NULL) = 6950 
970 6950 recvfrom(3, "$\0\0\0\2\0\0\0\1\0\0\0&\33\0\0\0\0\0\0d\0\0\0c\4\5\0\1\0\0\0"..., 8988, MSG_DONTWAIT, {sa_family=AF_NETLINK, pi  d=0, groups=00000000}, [12]) = 36 
971 6950 write(2, "sudo", 4)    = 4 
972 6950 write(2, ": ", 2)     = 2 
973 6950 write(2, "sorry, you must have a tty to ru"..., 38) = 38 
974 6950 write(2, "\n", 1)     = 1 
975 6950 setresuid(4294967295, 4294967295, 4294967295) = 0 
976 6950 setresgid(4294967295, 4294967295, 4294967295) = 0 
977 6950 exit_group(1)      = ? 
+0

당신은'Nagios :: Plugin 사용 '을 고려해 볼 수 있습니다. 왜냐하면이 모듈은 옵션 처리에 도움을 줄 수 있고 Nagios Plugin을 만드는 데 유용한 다른 것들이 있기 때문입니다. – dgw

답변

3

이것은 내가 루트 작동하지 않습니다/Nagios는/libexec 디렉토리/ 에서 스크립트의 사용 권한을 변경 해결 파일을 한

내 경우에는
Defaults:nagios !requiretty 
0

을/etc /의 sudoers에 다음을 추가하여 해결되었다 : root와 Nagios와 일 : nagios 사용자 권한!