您的位置:首页 > 运维架构 > Shell

shell script自动搭建简单的cacti监控

2013-03-04 17:20 447 查看
#!/bin/bash

#

#Shell name:config_cacti.sh

#

#Program:

# config cacti server by this shell script

#

#Author:perofu

#

#Mail: perofu.com@gmail.com

#

#History:

# 2012/10/23

#

#If you have any opinions please contact me

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:.

export PATH

soft_dir=/root/cacti/ #/full/path/

soft_rrdtool=/usr/local/src/rrdtool-1.4.5/ #NOTE:soft full name

soft_snmp=/usr/local/src/net-snmp-5.3.0.1/

soft_cacti=/usr/local/src/cacti-0.8.8a/

soft_http=/usr/local/apache2/htdocs/ #check where is your httpd home dir

cmd_mysql=$(which mysql)

file_config=/usr/local/apache2/htdocs/cacti/include/config.php

file_global=/usr/local/apache2/htdocs/cacti/include/global.php

#mysql root passwd

mysql_root_passwd=123456

#user name and password for cacti

user_name=cacti

user_passwd=123456

if [ $UID -ne "0" ]

then

echo "Run as root"

exit 77

fi

if [ ! -d ${soft_dir} ]

then

echo "It's not the ${soft_dir} exist"

exit 77

fi

if [ -z ${user_name} ]

then

echo "Please input a user name for cacti in config_cacti.sh"

exit 77

fi

if [ -z ${user_passwd} ]

then

echo "Please input a password for cacti in config_cacti.sh"

exit 77

fi

if [ -z ${mysql_root_passwd} ]

then

echo "Please input a password for mysql root in config_cacti.sh"

exit 77

fi

cd ${soft_dir}

#install base soft

yum install pango pango-devel freetype freetype-devel libpng libpng-devel gettext gettext-devel libjpeg libjpeg-devel gd gd-devel libxml2 libxml2-devel libiconv libiconv-devel qpixman qpixman-devel glib glib-devel cairo cairo-devel libart* make gcc gcc-c++ -y &> /dev/null

#taring soft to /usr/local/src/

for soft in $(ls *.tar.gz)

do

tar -axf ${soft} -C /usr/local/src/

done

#installing rrdtool

if [ ! -d ${soft_rrdtool} ]

then

echo "It's not the ${soft_rrdtool} exist"

exit 77

fi

cd ${soft_rrdtool}

./configure --prefix=/usr/local/rrdtool/ && make && make install

ln -s /usr/local/rrdtool/bin/* /usr/local/bin/

#installing net-snmp

if [ ! -d ${soft_snmp} ]

then

echo "It's not the ${soft_snmp} exist"

exit 77

fi

cd ${soft_snmp}

./configure --prefix=/usr/local/net-snmp/ && make && make install

ln -s /usr/local/net-snmp/bin/* /usr/local/bin/

cp EXAMPLE.conf /usr/local/net-snmp/share/snmp/snmpd.conf

/usr/local/net-snmp/sbin/snmpd -c /usr/local/net-snmp/share/snmp/snmpd.conf

echo "/usr/local/net-snmp/sbin/snmpd -c /usr/local/net-snmp/share/snmp/snmpd.conf" >> /etc/rc.local

#installing cacti

if [ ! -d ${soft_cacti} ]

then

echo "It's not the ${soft_cacti} exist"

exit 77

fi

cp -a ${soft_cacti} ${soft_http}/cacti

#NOTE:input you mysql user "root" and "password" in there

${cmd_mysql} -uroot -p${mysql_root_passwd} <<EOF

create database cacti;

grant all on cacti.* to "cacti"@"localhost" identified by "${user_passwd}";

flush privileges;

EOF

cd ${soft_http}/cacti/

${cmd_mysql} -uroot -p${mysql_root_passwd} cacti < cacti.sql &> /dev/null

##########################################################

#change config.php and global.php files

#input your cacti password to change last word as "cacti"

#config.php

#grep "\$database_username = \"cactiuser\";" ${file_config} |sed -i "/\$database_username = \"cactiuser\";/\$database_username = \"${user_name}\";/" ${file_config}

#grep "\$database_password = \"cactiuser\";" ${file_config} |sed -i "/\$database_password = \"cactiuser\";/\$database_password = \"${user_passwd}\";/" ${file_config}

#global.php

#grep "\$database_username = \"cactiuser\";" ${file_global} |sed -i "/\$database_username = \"cactiuser\";/\$database_username = \"${user_name}\";/" ${file_config}

#grep "\$database_password = \"cactiuser\";" ${file_global} |sed -i "/\$database_password = \"cactiuser\";/\$database_password = \"${user_passwd}\";/" ${file_global}

#####################################################

#config.php

ed -s ${file_config} <<EOF

g/\$database_username = "cactiuser";/s/\$database_username = "cactiuser";/\$database_username = "${user_name}";/g

w

q

EOF

ed -s ${file_config} <<EOF

g/\$database_password = "cactiuser";/s/\$database_password = "cactiuser";/\$database_password = "${user_passwd}";/g

w

q

EOF

#global.php

ed -s ${file_global} <<EOF

g/\$database_username = "cactiuser";/s/\$database_username = "cactiuser";/\$database_username = "${user_name}";/g

w

q

EOF

ed -s ${file_global} <<EOF

g/\$database_password = "cactiuser";/s/\$database_password = "cactiuser";/\$database_password = "${user_passwd}";/g

w

q

EOF

chown -R root:root /usr/local/apache2/htdocs/cacti

echo '*/5 * * * * php /usr/local/apache2/htdocs/cacti/poller.php &> /dev/null' >> /etc/crontab

echo "Please login [http://localhost/cacti/] to finsh setting"

echo "Enjoy cacti..."

cat <<EOF

##########################Note####################

login:

username: admin

password: admin

##################################################

EOF

本文出自 “无咎” 博客,请务必保留此出处http://perofu.blog.51cto.com/6061242/1144582
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: