最新消息:走过的,离开的,已经错过,新开始2016

lnmp 安装nagios

技术随笔 果果 5305浏览 1评论

之前果果介绍了 lnmp下面安装cacti,一台服务器只放一个cacit有一些浪费,这次果果继续在上面安装nagios。

nagios1

lnmp的安装就不介绍了。

nagios是cgi方式运行,nginx是不支持的,所以我们要以FastCGI运行。

1、安装FCGI

Www.GuoGuo.iT
1
2
3
4
5
6
wget http://search.cpan.org/CPAN/authors/id/F/FL/FLORA/FCGI-0.73.tar.gz
tar xvzf FCGI-0.73.tar.gz
cd FCGI-0.73
perl Makefile.PL
make
make install

2、安装FCGI-ProcManager

Www.GuoGuo.iT
1
2
3
4
5
6
wget http://search.cpan.org/CPAN/authors/id/G/GB/GBJK/FCGI-ProcManager-0.19.tar.gz
tar xvzf FCGI-ProcManager-0.19.tar.gz
cd FCGI-ProcManager-0.19
perl Makefile.PL
make
make install

安装IO模块

Www.GuoGuo.iT
1
2
3
4
5
6
7
wget http://search.cpan.org/CPAN/authors/id/G/GB/GBARR/IO-1.25.tar.gz
tar zxvf IO-1.25.tar.gz
cd IO-1.25
perl Makefile.PL
make
make install
cd ..

安装IO::ALL 模块

Www.GuoGuo.iT
1
2
3
4
5
6
7
wget http://search.cpan.org/CPAN/authors/id/I/IN/INGY/IO-All-0.81.tar.gz
tar zxvf IO-All-0.41.tar.gz
cd IO-All-0.41
perl Makefile.PL
make
make install
cd ..

3、安装Perl脚本

这个脚本是执行Perl关键,果果修改过,解决在nagios 提交的时候502错误。

这个可以直接放在nginx目录

Www.GuoGuo.iT
1
vi /usr/local/nginx/perl-fcgi.pl
Www.GuoGuo.iT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/perl
#
#   author      Daniel Dominik Rudnicki
#   thanks to:  Piotr Romanczuk
#   email       daniel@sardzent.org
#   version     0.4.3
#   webpage     http://www.nginx.eu/
#
#   BASED @ http://wiki.codemongers.com/NginxSimpleCGI
#
#
# use strict;
use FCGI;
use Getopt::Long;
use IO::All;
use Socket;
 
sub init {
    GetOptions( "h" => \$help,
            "verbose!"=>\$verbose,
            "pid=s" => \$filepid,
            "l=s" => \$logfile,
            "S:s"   => \$unixsocket,
            "P:i"   => \$unixport) or usage();
        usage() if $help;
 
    print " Starting Nginx-fcgi\n" if $verbose;
    print " Running with $> UID" if $verbose;
    print " Perl $]" if $verbose;
 
    if ( $> == "0" ) {
        print "\n\tERROR\tRunning as a root!\n";
        print "\tSuggested not to do so !!!\n\n";
        exit 1;
    }
 
        if ( ! $logfile ) {
        print "\n\tERROR\t log file must declared\n"
            . "\tuse $0 with option -l filename\n\n";
        exit 1;
    }
    print " Using log file $logfile\n" if $verbose;
    "\n\n" >> io($logfile);
    addlog($logfile, "Starting Nginx-cfgi");
    addlog($logfile, "Running with $> UID");
    addlog($logfile, "Perl $]");
    addlog($logfile, "Testing socket options");
 
    if ( ($unixsocket && $unixport) || (!($unixsocket) && !($unixport)) ) {
        print "\n\tERROR\tOnly one option can be used!\n";
        print "\tSuggested (beacuse of speed) is usage UNIX socket -S \n\n";
        exit 1;
    }
 
    if ($unixsocket) {
        print " Daemon listening at UNIX socket $unixsocket\n" if $versbose;
        addlog($logfile, "Deamon listening at UNIX socket $unixsocket");
    } else {
        print " Daemon listening at TCP/IP socket *:$unixport\n" if $verbose;
        #
        addlog($logfile, "Daemon listening at TCP/IP socket *:$unixport");
    }
 
    if ( -e $filepid ) {
        print "\n\tERROR\t PID file $filepid already exists\n\n";
        addlog($logfile, "Can not use PID file $filepid, already exists.");
        exit 1;
    }
 
    if ( $unixsocket ) {
        print " Creating UNIX socket\n" if $verbose;
        $socket = FCGI::OpenSocket( $unixsocket, 10 );
        if ( !$socket) {
            print " Couldn't create socket\n";
            addlog($logfile, "Couldn't create socket");
            exit 1;
        }
        print " Using UNIX socket $unixsocket\n" if $verbose;
    } else {
        print " Creating TCP/IP socket\n" if $verbose;
        $portnumber = ":".$unixport;
        $socket = FCGI::OpenSocket( $unixport, 10 );
        if ( !$socket ) {
            print " Couldn't create socket\n";
            addlog($logfile, "Couldn't create socket");
            exit 1;
        }
        print " Using port $unixport\n" if $verbose;
    }
    addlog($logfile, "Socket created");
 
    if ( ! $filepid ) {
        print "\n\tERROR\t PID file must declared\n"
            . "\tuse $0 with option -pid filename\n\n";
        exit 1;
    }
    print " Using PID file $filepid\n" if $verbose;
    addlog($logfile, "Using PID file $filepid");
 
    my $pidnumber = $$;
    $pidnumber > io($filepid);
    print " PID number $$\n" if $verbose;
    addlog($logfile, "PID number $pidnumber");
     
}
 
sub addzero {
    my ($date) = shift;
    if ($date < 10) {
        return "0$date";
    }
       return $date;
}
 
sub logformat {
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$iddst) = localtime(time);
    my $datestring;
    $year += 1900;
    $mon++;
    $mon  = addzero($mon);
    $mday = addzero($mday);
    $min  = addzero($min);
    $datestring = "$year-$mon-$mday $hour:$min";
    return($datestring);
}
 
sub addlog {
    my ($log_file, $log_message) = @_;
    my $curr_time = logformat();
    my $write_message = "[$curr_time]   $log_message";
    $write_message >> io($log_file);
    "\n" >> io($log_file);
}
 
sub printerror {
    my $message = @_;
    print "\n   Nginx FastCGI\tERROR\n"
        . "\t $message\n\n";
    exit 1;
}
 
sub usage {
    print "\n   Nginx FastCGI \n"
        . "\n\tusage: $0 [-h] -S string -P int\n"
        . "\n\t-h\t\t: this (help) message"
        . "\n\t-S path\t\t: path for UNIX socket"
        . "\n\t-P port\t\t: port number"
        . "\n\t-p file\t\t: path for pid file"
        . "\n\t-l file\t\t: path for logfile"
        . "\n\n\texample: $0 -S /var/run/nginx-perl_cgi.sock -l /var/log/nginx/nginx-cfgi.log -pid /var/run/nginx-fcgi.pid\n\n";
    exit 1;
}
 
 
init;
#
END() { } BEGIN() { }
*CORE::GLOBAL::exit = sub { die "fakeexit\nrc=".shift()."\n"; }; eval q{exit};
if ($@) {
    exit unless $@ =~ /^fakeexit/;
} ;
 
# fork part
my $pid = fork();
 
if( $pid == 0 ) {
    &main;
    exit 0;
}
 
print " Forking worker process with PID $pid\n" if $verbose;
addlog($logfile, "Forking worker process with PID $pid");
print " Update PID file $filepid\n" if $verbose;
addlog($logfile, "Update PID file $filepid");
$pid > io($filepid);
print " Worker process running.\n" if $verbose;
addlog ($logfile, "Parent process $$ is exiting");
exit 0;
 
sub main {
    $request = FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%req_params, $socket );
    if ($request) { request_loop()};
        FCGI::CloseSocket( $socket );
}
 
sub request_loop {
    while( $request->Accept() >= 0 ) {
        # processing any STDIN input from WebServer (for CGI-POST actions)
        $stdin_passthrough = '';
        $req_len = 0 + $req_params{'CONTENT_LENGTH'};
        if (($req_params{'REQUEST_METHOD'} eq 'POST') && ($req_len != 0) ){
            while ($req_len) {
                $stdin_passthrough .= getc(STDIN);
                $req_len--;
            }
        }
 
        # running the cgi app
        if ( (-x $req_params{SCRIPT_FILENAME}) &&
            (-s $req_params{SCRIPT_FILENAME}) &&
            (-r $req_params{SCRIPT_FILENAME})
        ){
            foreach $key ( keys %req_params){
                $ENV{$key} = $req_params{$key};
            }
            if ( $verbose ) {
                addlog($logfile, "running $req_params{SCRIPT_FILENAME}");
            }
            # http://perldoc.perl.org/perlipc.html#Safe-Pipe-Opens
            #
            ###20160120 liaoxg
            #open $cgi_app, '-|', $req_params{SCRIPT_FILENAME}, $stdin_passthrough or print("Content-type: text/plain\r\n\r\n"); print "Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !\n"; # addlog($logfile, "Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !");
            open $cgi_app, '-|', "echo '$stdin_passthrough' | '$req_params{SCRIPT_FILENAME}'" or print("Content-type: text/plain\r\n\r\n"); print "Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !\n"; # addlog($logfile, "Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !");          
 
 
            if ($cgi_app) {
                print <$cgi_app>;
                close $cgi_app;
            }
        } else {
            print("Content-type: text/plain\r\n\r\n");
            print "Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not exist or is not executable by this process.\n";
            addlog($logfile, "Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not exist or is not executable by this process.");
        }
    }
}

修改权限

Www.GuoGuo.iT
1
chmod 755 /usr/local/nginx/perl-fcgi.pl

4、建立一个CGI启动/停止脚本

这个SHELL脚本只是为了方便管理上面的Perl脚本。脚本中的www为nginx的运行用户,请据自己的实际情况调整。

注意事项:不能用root用户执行(会提示). 要用与Nginx相同身份的用户执行。否则可能会在Nginx Log中提示 Permision Denied。

Www.GuoGuo.iT
1
vi /usr/local/nginx/start_perl_cgi.sh
Www.GuoGuo.iT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/bin/bash
#set -x
dir=/usr/local/nginx/
   
stop ()
{
#pkill  -f  $dir/perl-fcgi.pl
kill $(cat $dir/logs/perl-fcgi.pid)
rm $dir/logs/perl-fcgi.pid 2>/dev/null
rm $dir/logs/perl-fcgi.sock 2>/dev/null
echo "stop perl-fcgi done"
}
   
start ()
{
rm $dir/now_start_perl_fcgi.sh 2>/dev/null
   
chown www.www $dir/logs
echo "$dir/perl-fcgi.pl -l $dir/logs/perl-fcgi.log -pid $dir/logs/perl-fcgi.pid -S $dir/logs/perl-fcgi.sock" >>$dir/now_start_perl_fcgi.sh
   
chown www.www $dir/now_start_perl_fcgi.sh
chmod u+x $dir/now_start_perl_fcgi.sh
   
sudo -u www $dir/now_start_perl_fcgi.sh
echo "start perl-fcgi done"
}
   
case $1 in
stop)
stop
;;
start)
start
;;
restart)
stop
start
;;
esac

修改脚本权限,使之可以执行

Www.GuoGuo.iT
1
chmod 755 /usr/local/nginx/start_perl_cgi.sh

启动perl_cgi

Www.GuoGuo.iT
1
/usr/local/nginx/start_perl_cgi.sh start

5、Nagios安装(服务端)

下载

Www.GuoGuo.iT
1
2
3
wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.1.1.tar.gz
 
wget http://nagios-plugins.org/download/nagios-plugins-2.1.1.tar.gz
Www.GuoGuo.iT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
useradd -s /sbin/nologin nagios
groupadd nagcmd
usermod -a -G nagcmd nagios
usermod -a -G nagcmd www
tar xzf nagios-4.1.1.tar.gz
cd nagios
yum -y install gd-devel
./configure --prefix=/usr/local/nagios --with-command-group=nagcmd
make all
make install #用于安装主要的程序、CGI及HTML文件
make install-init #用于生成init启动脚本
make install-config #用于安装示例配置文件
make install-commandmode #用于设置相应的目录权限
chkconfig --add nagios
chkconfig nagios on
cd ../

安装插件

Www.GuoGuo.iT
1
2
3
4
5
6
tar xzf nagios-plugins-2.1.1.tar.gz
cd nagios-plugins-1.4.16
./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios
make
make install
cd ../

6、Nagios客户端安装

Www.GuoGuo.iT
1
2
3
4
5
6
7
8
9
10
wget http://nchc.dl.sourceforge.net/project/nagios/nrpe-2.x/nrpe-2.14/nrpe-2.14.tar.gz
 
tar xzf nrpe-2.14.tar.gz
cd nrpe-2.14
./configure
make
make install
cp sample-config/nrpe.cfg /usr/local/nagios/etc/
chown nagios.nagios /usr/local/nagios/etc/nrpe.cfg
cd ../

7、nginx站点配置

新建一个站点,在nginx.conf 最后加入

Www.GuoGuo.iT
1
include nagios.conf;
Www.GuoGuo.iT
1
vi nagios.conf

里面加入这些内容,果果用的是8899端口,注意修改路径,果果是安装在 /usr/local/nagios/

Www.GuoGuo.iT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
server {
listen 8899;
server_name 192.168.1.238;
access_log logs/nagios_access.log combined;
auth_basic "Nagios Access";
auth_basic_user_file /usr/local/nagios/etc/htpasswd.users;
location / {
root /usr/local/nagios/share;
index index.html index.htm index.php;
}
location ~ .*\.(php|php5)?$ {
root /usr/local/nagios/share;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location /nagios {
alias /usr/local/nagios/share;
}
location /cgi-bin/images {
alias /usr/local/nagios/share/images;
}
location /cgi-bin/stylesheets {
alias /usr/local/nagios/share/stylesheets;
}
location /cgi-bin {
alias /usr/local/nagios/sbin;
}
location ~ .*\.(cgi|pl)?$ {
gzip off;
root /usr/local/nagios/sbin;
rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;
fastcgi_pass unix:/usr/local/nginx/logs/perl-fcgi.sock;
fastcgi_param SCRIPT_FILENAME /usr/local/nagios/sbin$fastcgi_script_name;
fastcgi_index index.cgi;
fastcgi_param REMOTE_USER $remote_user;
fastcgi_param HTTP_ACCEPT_LANGUAGE en_US;
include fastcgi_params;
fastcgi_read_timeout 60;
}

修改nagios的登陆用户密码,这里需要用到apache的 htpasswd,

htpasswd.users记录了密码,下面是 admin用户 密码 123456

Www.GuoGuo.iT
1
2
htpasswd -nb admin 123456 > /usr/local/nagios/etc/htpasswd.users
chown nagios.nagios /usr/local/nagios/etc/htpasswd.users

如果没有apache,我们可以利用在线生成工具

http://tool.oschina.net/htpasswd

最后用 命令测试nginx有没有错误

Www.GuoGuo.iT
1
2
3
4
#/usr/local/nginx/sbin/nginx -t
 
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

没有错误,重启nginx,用网页就可以看到nagios的页面。

好了,安装完成了,下一篇果果再来介绍nagios的配置,果果把能想到的都说到了,但难免有错误,欢迎大家指出,果果参考了很多教程

http://www.open-open.com/lib/view/1328680567780

转载请注明:果果.IT » lnmp 安装nagios

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址