nagios+LAMP自动安装脚本

#!/bin/sh 
# Nagios V3.03 install script 
# Write by zhengyp 
#Date:2008-10-8 


# Softwares are all store in /usr/local/src 
cd /usr/local/src 
#Install LAMP for Nagios 

# Step 1. Install Apache 
tar zxvf httpd-2.2.9.tar.gz 
cd httpd-2.2.9 
./configure --prefix=/usr/local/apache --enable-so --enable-mods-shared=all 
make && make install 
#Configure the apache server for PHP 
sed -i 's#DirectoryIndex index.html#& index.php#' /usr/local/apache/conf/http.conf 
sed -i '/AddType application\/x-gzip .gz .tgz/i\AddType application/x-httpd-php .php\ 
AddType application/x-httpd-php-source .phps' /usr/local/apache/conf/http.conf 

#Step 2. Install Mysql As a server 
#Add a mysql user 
/usr/sbin/groupadd mysql 
/usr/sbin/useradd -g mysql mysql 
cd .. 
tar zxvf mysql-5.1.26-rc.tar.gz 
cd mysql-5.1.26-rc/ 
./configure --prefix=/usr/local/mysql/ --without-debug --with-unix-socket-path=/usr/local/mysql/mysql.sock --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static --enable-assembler --with-extra-charsets=gbk,gb2312,utf8 --with-pthread --enable-thread-safe-client 
make && make install 
chmod +w /usr/local/mysql 
chown -R mysql:mysql /usr/local/mysql 
cp support-files/my-medium.cnf /usr/local/mysql/my.cnf 
cd ../ 

#Initialize the database 
#You can change the data directory by set "--datadir=" 
#mkdir /data 
#chown -R mysql:mysql /data 

/usr/local/mysql/bin/mysql_install_db --defaults-file=/usr/local/mysql/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql --pid-file=/usr/local/mysql/mysql.pid --skip-locking --port=3306 --socket=/tmp/mysql.sock 
#Start Mysql 
/bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/usr/local/mysql/my.cnf & 

#Step 3. Install PHP 
#If you use CentOS, Please run yum to check that the graphics library have installed.Also it will update for these softwares 
#If you use Redhat , Install the graphics library by "rpm" command 
cd .. 
yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libpng libpng-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc- devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel 

tar zxvf php-5.2.6.tar.gz 
cd php-5.2.6 
./configure --prefix=/usr/local/php5 --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-apxs2=/usr/local/apache/bin/apxs --with-freetype --with-jpeg --with-png --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --with-curl --with-curlwrappers --with-gd --enable-gd-native-ttf --with-openssl 
make && make install 
cd .. 

#Step 4.Install Nagios 3.03 
#Add nagios user 
adduser nagios 
mkdir /usr/local/nagios 
chown nagios:nagios /usr/local/nagios 
groupadd nagcmd 
usermod -G nagcmd nagios 
tar zxvf nagios-3.0.3.tar.gz 
cd nagios-3.0.3 
sed -i 's#wav#x-mplayer2#' cgi/status.c 
#Configure and make 
./configure --prefix=/usr/local/nagios --with-command-group=nagcmd --with-httpd-conf=/usr/local/apache/conf/httpd.conf --with-gd 
make all 
make install 
make install-init 
make install-config 
make install-commandmode 

#Add a user for nagios ,You can change the passwd for the user you add 
/usr/local/apache/bin/htpasswd -c /usr/local/nagios/etc/htpasswd nagios <<EOF 
123 
123 
EOF 

sed -i '/AddType application\/x-gzip .gz .tgz/i\AddType application/x-httpd-php .php\ 
AddType application/x-httpd-php-source .phps' /usr/local/apache/conf/http.conf 

sed -i '/<\/IfModule>/a\Alias /nagios/cgi-bin/images/ "/usr/local/nagios/share/images/"\ 
<Directory "/usr/local/nagios/share/images/">\ 
    AllowOverride None\ 
    Options None\ 
    Order allow,deny\ 
    Allow from all\ 
    AuthType Basic\ 
    AuthName "Login User"\ 
    AuthUserFile /usr/local/nagios/etc/htpasswd\ 
    require user nagios\ 
</Directory>\ 
ScriptAlias /nagios/cgi-bin/ "/usr/local/nagios/sbin/"\ 
<Directory "/usr/local/nagios/sbin/">\ 
AllowOverride None\ 
    Options None\ 
    Order allow,deny\ 
    Allow from all\ 
    AuthType Basic\ 
    AuthName "Login User"\ 
    AuthUserFile /usr/local/nagios/etc/htpasswd\ 
    require user nagios\ 
</Directory>\ 
Alias /nagios/ "/usr/local/nagios/share/"\ 
<Directory "/usr/local/nagios/share/">\ 
    AllowOverride None\ 
    Options None\ 
    Order allow,deny\ 
    Allow from all\ 
    AuthType Basic\ 
    AuthName "Login User"\ 
    AuthUserFile /usr/local/nagios/etc/htpasswd\ 
    require user nagios\ 
</Directory>' /usr/local/apache/conf/http.conf 
sed -i 's#ServerName www.temple.com:80#ServerName 127.0.0.1#' /usr/local/apache/conf/http.conf 
/usr/local/apache/bin/apachectl start 

#Step 5. Install Naigos plugins 
#Install plugins 
cd /usr/local/src 
tar xvzf nagios-plugins-1.4.12.tar.gz 
cd nagios-plugins-1.4.11 
./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios --enable-redhat-pthread-workaround --with-nagios-user=nagios --with-mysql=/usr/local/mysql 
make && make install 

# Install nrpe 
cd /usr/local/src 
tar zxvf nrpe-2.12.tar.gz 
cd nrpe-2.12 
./configure --enable-ssl --with-ssl-lib=/usr/local/nagios/openssl/lib --with-kerberos-inc=/usr/local/nagios/openssl/include --enable-command-args 
make;make install 
make install-plugin 
make install-daemon 
make install-daemon-config 
chown nagios:nagios /usr/local/nagios/etc/nrpe.cfg 
sed 's#dont_blame_nrpe=0#dont_blame_nrpe=1#' /usr/local/nagios/etc/nrpe.cfg 
#Add nrpe to service 
echo "nrpe 5666/tcp # NRPE" >>/etc/service 
echo " " > /etc/xinetd.d/nrpe 
sed -i '/ /i\service nrpe\ 
{\ 
disable     =   no\ 
flags       =   REUSE\ 
socket_type =   stream\ 
wait        =   no\ 
user        =   nagios\ 
server      =   /usr/local/nagios/bin/nrpe\ 
server_args =   -c /usr/local/nagios/etc/nrpe.cfg --inetd\ 
log_on_failure += USERID\ 
only_from = 192.168.0.200 #nagiosServer IP\ 
}' /etc/xinetd.d/nrpe 

#Step 6. Configure Naigos 
#Add the user who you've create in step 4 by htpasswd for nagios 
cd /usr/local/nagios/etc 
sed -i 's#default_user_name=guest#default_user_name=nagios' cgi.cfg 
sed -i 's#=nagiosadmin#=nagiosadmin,nagios' cgi.cfg 
#Start nagios 
/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg 
service nagios start 

#Step 7 . Set all servers start after system boot 
echo "/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg" >> /etc/rc.local 
echo "service nagios start" >> /etc/rc.local 
echo "/bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/usr/local/mysql/my.cnf &" >> /etc/rc.local 
echo "/usr/local/apache/bin/apache/bin/apachectl start" >> /etc/rc.local 

posted @ 2011-11-02 10:12  潺莪  阅读(278)  评论(0编辑  收藏  举报