cacti+Nagios一键部署脚本

cacti+Nagios一键部署脚本

 

#!/bin/bash
echo"###此脚本执行完之后的,所有密码均为123.com 也可以自己定义,需要修改脚本#######"
mariadburl="/etc/my.cnf.d/cacti.cnf"

cat /etc/hosts  | grep 'mirrors.9test.org'
if [ X$? != "X0" ]
then
  chattr -i /etc/hosts
  echo "x.x.x.x download.baidu.com " >> /etc/hosts
fi

#+++install httpd+++#
yum install -y httpd httpd-devel &> /dev/null
systemctl enable httpd.service &> /dev/null
systemctl start httpd.service
if [ $? -ne 0 ];then
	echo "faill"
else
	echo "success"
fi
#+++install Mairadb+++#
yum install -y mariadb mariadb-server mariadb-devel &> /dev/null

echo "[mysqld]" > $mariadburl
echo "bind-address = 127.0.0.1" >> $mariadburl
echo "default-storage-engine = innodb" >> $mariadburl
echo "innodb_file_per_table" >> $mariadburl
echo "max_connections = 4096" >> $mariadburl
echo "collation-server = utf8_general_ci" >> $mariadburl
echo "character-set-server = utf8" >> $mariadburl

systemctl enable mariadb.service &> /dev/null
systemctl start  mariadb.service
if [ $? -ne 0 ];then
	echo "fail"
else
	echo "success"
fi

mysql_secure_installation  ##----初始化MariaDB数据库密码,这里需要将密码更新为123.com

mysql -uroot -p123.com -e "create database cacti"
mysql -uroot -p123.com -e "grant all on cacti.* to cacti@localhost identified by '123.com'" 
mysql -uroot -p123.com -e "flush privileges"
if [ $? -ne 0 ];then
	echo "fail"
else
	echo "success"
fi

#+++install PHP+++#
yum install -y install php-mysql php-pear php-common php-gd php-devel php php-mbstring php-cli &> /dev/null
#+++install php-snmp、net-snmp+++#
yum install php-snmp net-snmp-utils net-snmp-libs net-snmp-devel -y &> /dev/null
systemctl enable snmpd.service &> /dev/null
systemctl start  snmpd.service

#+++install rrdtool+++#
yum install rrdtool -y &> /dev/null

#+++download and install cacti+++#
yum install wget gcc -y &> /dev/null
wget http://www.cacti.net/downloads/cacti-0.8.8h.tar.gz -O /usr/local/cacti-0.8.8h.tar.gz &> /dev/null
cd /usr/local/
tar zxf cacti-0.8.8h.tar.gz && mv cacti-0.8.8h cacti
rm -fr cacti-0.8.8h.tar.gz

sed -i '26c $database_type = "mysql";' /usr/local/cacti/include/config.php
sed -i '27c $database_default = "cacti";' /usr/local/cacti/include/config.php
sed -i '28c $database_hostname = "localhost";' /usr/local/cacti/include/config.php
sed -i '29c $database_username = "cacti";' /usr/local/cacti/include/config.php
sed -i '30c $database_password = "123.com";' /usr/local/cacti/include/config.php
sed -i '31c $database_port = "3306";' /usr/local/cacti/include/config.php
sed -i '32c $database_ssl = false;' /usr/local/cacti/include/config.php
if [ $? -ne 0 ];then
	echo "fail"
else
	echo "success"
fi

useradd -d /usr/local/cacti -m cacti
chown -R cacti /usr/local/cacti/
chgrp -R cacti /usr/local/cacti
chmod -R 755 /usr/local/cacti/
mysql -ucacti -p123.com cacti < /usr/local/cacti/cacti.sql 

wget http://download.baidu.com/rpm/cacti.conf.tar.gz -O /etc/httpd/conf.d/cacti.conf.tar.gz &> /dev/null
cd /etc/httpd/conf.d/ && tar zxf cacti.conf.tar.gz
rm -fr cacti.conf.tar.gz
if [ $? -ne 0 ];then
	echo "fail"
else
	echo "success"
fi

systemctl restart httpd.service
systemctl restart mariadb.service
echo "*/5 * * * *    cacti   /usr/bin/php /usr/local/cacti/poller.php > /dev/null 2>&1" >> /etc/cron.d/cacti

#+++insta spine+++#
wget http://www.cacti.net/downloads/spine/cacti-spine-0.8.8h.tar.gz &> /dev/null
tar zxvf cacti-spine-0.8.8h.tar.gz &> /dev/null
cd cacti-spine-0.8.8h
rm -fr cacti-spine-0.8.8h.tar.gz
./configure && make && make install &> /dev/null
cp /usr/local/spine/etc/spine.conf.dist /usr/local/spine/etc/spine.conf

sed -i '33c DB_Host         localhost'  /usr/local/spine/etc/spine.conf
sed -i '34c DB_Database     cacti'      /usr/local/spine/etc/spine.conf
sed -i '35c DB_User         cacti'      /usr/local/spine/etc/spine.conf
sed -i '36c DB_Pass         123.com'    /usr/local/spine/etc/spine.conf
sed -i '37c DB_Port         3306'       /usr/local/spine/etc/spine.conf

/usr/local/spine/bin/spine
cp /usr/local/spine/etc/spine.conf /etc/spine.conf
/usr/local/spine/bin/spine 
if [ $? -ne 0 ];then
	echo "fail"
else
	echo "success"
fi

yum provides snmpwalk #----查看文件屬於那個包

#+++insta nagios+++#
yum install httpd php php-cli gcc glibc glibc-common gd gd-devel net-snmp openssl-devel wget unzip -y &> /dev/null

useradd nagios
groupadd nagcmd
usermod -a -G nagcmd nagios
usermod -a -G nagcmd apache

wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.1.1.tar.gz  &> /dev/null
tar zxvf nagios-4.1.1.tar.gz
rm -fr nagios-4.1.1.tar.gz
cd nagios-4.1.1
./configure --with-command-group=nagcmd &> /dev/null
make all 								&> /dev/null
make install							&> /dev/null
make install-init						&> /dev/null
make install-config						&> /dev/null
make install-commandmode				&> /dev/null
make install-webconf					&> /dev/null
sed -i '163c <IfModule dir_module>' /etc/httpd/conf/httpd.conf
sed -i '164c     DirectoryIndex index.html,index.php' /etc/httpd/conf/httpd.conf
sed -i '165c </IfModule>' /etc/httpd/conf/httpd.conf

htpasswd -c /usr/local/nagios/etc/htpasswd.users cloud #-----創建web訪問用戶名和密碼

wget http://nagios-plugins.org/download/nagios-plugins-2.1.1.tar.gz				  &> /dev/null
tar zxf nagios-plugins-2.1.1.tar.gz
rm -fr nagios-plugins-2.1.1.tar.gz
cd nagios-plugins-2.1.1
./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-openssl  &> /dev/null
make all  &> /dev/null
make install  &> /dev/null
service nagios start
chkconfig nagios on
chkconfig nagios --list

if [ $? -ne 0 ];then
	echo "fail"
else
	echo "success"
fi

systemctl restart httpd.service
systemctl restart mariadb.service

cat cacti.conf

Alias /cacti	/usr/local/cacti
<Directory /usr/local/cacti>
	<IfModule mod_authz_core.c>
# httpd 2.4.6
Require all granted
	</IfModule>
<IfModule !mod_authz_core.c>
# httpd 2.2
Order deny,allow
Deny from all
Allow from all
	</IfModule>
</Directory>v

  

posted @ 2019-04-23 17:54  Boks  阅读(503)  评论(0)    收藏  举报