您现在的位置是: 网站首页 >Shell Shell

Debian10部署Cacti1.2.18脚本

admin2021年8月1日 13:03 Linux | MySQL 1139人已围观

# Debian10部署Cacti监控 Cacti下载地址: https://files.cacti.net/cacti/linux/ https://files.cacti.net/spine/ 安装文档: https://docs.cacti.net/Installing-Under-Ubuntu-Debian.md#create-your-cron-task-file-or-systemd-units-file ## 部署Cacti脚本(全安装/升级过程) [install_or_upgrade_cacti_1.2.18.sh](@Scripts/install_or_upgrade_cacti_1.2.18.sh) ```bash #!/bin/bash # info: test debian10 # mysql root密码 MYSQL_ROOT_PASSWORD='root' # cacti版本 CACTI_VERSION=cacti-1.2.17 # cacti升级版本 CACTI_VERSION_NEW=cacti-1.2.18 # cacti spine版本 CACTI_SPINE_VERSION=cacti-spine-1.2.17 # 文件存放路径 CACTI_PATH="/opt/cacti" # cacti 工作路径 CACTI_WORKDIR="/var/www/html/cacti" echo "========================> 手动配置软件源" apt-get update installApache(){ echo echo echo "========================> 安装apache" apt-get install apache2 -y # 安装完,访问 http://IP 会显示“Apache2 Debian Default Page” # 默认html根目录 /var/www/ echo "========================> apache安装版本号如下" apache2 -v # Server version: Apache/2.4.38 (Debian) # Server built: 2021-06-10T10:13:06 } installMySQL(){ echo echo echo "========================> 安装mysql" apt-get install mariadb-server -y mysql -V # mysql Ver 15.1 Distrib 10.3.29-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2 echo "========================> 设置mysql root密码" # mysql -e "set password for root@localhost = password('root');" mysqladmin password $MYSQL_ROOT_PASSWORD } installPHP(){ echo echo echo "========================> 安装php7.2" apt -y install software-properties-common apt-transport-https lsb-release ca-certificates wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg sh -c 'echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list' apt-get update apt install php7.2-fpm php7.2-mysql -y echo "========================> 安装php相应的扩展" # libapache2-mod-php7.2 必须安装才能解析php代码 apt install libapache2-mod-php7.2 php7.2-curl -y # sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/' /etc/php/7.2/fpm/php.ini systemctl restart php7.2-fpm php -version # PHP 7.2.34-23+0~20210701.63+debian10~1.gbpd7cd48 (cli) (built: Jul 1 2021 16:12:59) ( NTS ) # Copyright (c) 1997-2018 The PHP Group # Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies # with Zend OPcache v7.2.34-23+0~20210701.63+debian10~1.gbpd7cd48, Copyright (c) 1999-2018, by Zend Technologies # 创建测试文件 tee /var/www/html/info.php << EOF <?php phpinfo(); ?> EOF # 访问 http://ip/info.php } installTools(){ echo echo echo "========================> 安装snmp snmpd" # 关系着数据的收集 apt-get install snmp snmpd -y echo "========================> 安装rrdtool" # snmp抓到数据不是储存在mysql 中,而是存在rrdtool生成的rrd文件中,rrdtool的作用只是存储数据和画图 apt-get install rrdtool -y echo "========================> 配置时间" rm /etc/localtime ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime date } installCactiPkg(){ echo echo echo "========================> 安装cacti" if [ ! -d $CACTI_PATH ];then mkdir -p $CACTI_PATH fi cd $CACTI_PATH if [ ! -f $CACTI_VERSION.tar.gz ];then wget https://files.cacti.net/cacti/linux/$CACTI_VERSION.tar.gz fi # 解压后将其复制到Apache工作目录 tar -zxf $CACTI_VERSION.tar.gz -C $CACTI_PATH cp -rpf $CACTI_PATH/$CACTI_VERSION $CACTI_WORKDIR # 查看Cacti连接数据库默认的帐密,下方先按照这个帐密授权数据库 cat $CACTI_WORKDIR/include/config.php | egrep '\$database_' # $database_type = 'mysql'; # $database_default = 'cacti'; # $database_hostname = 'localhost'; # $database_username = 'cactiuser'; # $database_password = 'cactiuser'; # $database_port = '3306'; # $database_retries = 5; # $database_ssl = false; # $database_ssl_key = ''; # $database_ssl_cert = ''; # $database_ssl_ca = ''; } configConnMySQL(){ echo echo echo "========================> 根据config.php配置mysql连接" mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "create database cacti;" mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "show databases;" # 第三个参数表示指定数据库 mysql -uroot -p${MYSQL_ROOT_PASSWORD} cacti < $CACTI_WORKDIR/cacti.sql # 登陆mysql,设置用户权限 mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "show databases;" mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "GRANT ALL ON cacti.* TO cactiuser@localhost IDENTIFIED BY 'cactiuser'; flush privileges" # 配置Cacti使用数据库,如果这儿密码有变化,还需要修改 $CACTI_WORKDIR/include/config.php 密码配置 # 重启apache,就可以生效。 systemctl restart apache2 } configCronJob(){ echo echo echo "========================> 设置cron job,运行cacti轮循器" # 设置权限 cd $CACTI_WORKDIR chown -R www-data:www-data rra/ log/ # 创建任务计划 tee /etc/cron.d/cacti << EOF */5 * * * * www-data /usr/bin/php $CACTI_WORKDIR/poller.php > /dev/null 2>&1 EOF systemctl restart cron.service # 这个地方一定要注意,由于我们是使用www-data这个用户运行和rra的目录权限是属于www-data,我们不能直接用root的权限去运行下面这个命令`php $CACTI_WORKDIR/poller.php > /dev/null 2>&1` # 这样会导致创建的rrd文件是属于root的,导致没有数据。所以大家装完后,耐心等待5分钟,就可以看到图和数据。 } configSystemd(){ echo echo echo "========================> 配置service,运行cacti轮循器" # 对于 systemd 单元的文件安装,需要修改包含的单元文件以遵循您的安装位置和所需的用户和组来运行 Cacti 轮询器。 mkdir -p /etc/sysconfig touch /etc/sysconfig/cactid ln -s $CACTI_WORKDIR/service/cactid.service /etc/systemd/system/cactid.service systemctl daemon-reload systemctl enable cactid systemctl start cactid systemctl status cactid } optimizedConfig(){ echo echo echo "========================> 优化配置:PHP - Recommendations (web)" cat /etc/php/7.2/apache2/php.ini | grep memory_limit # memory_limit = 128M sed -ri /^"memory_limit ="/c"memory_limit = 400M" /etc/php/7.2/apache2/php.ini cat /etc/php/7.2/apache2/php.ini | grep memory_limit # memory_limit = 400M cat /etc/php/7.2/apache2/php.ini | grep max_execution_time sed -ri /^"max_execution_time ="/c"max_execution_time = 60" /etc/php/7.2/apache2/php.ini cat /etc/php/7.2/apache2/php.ini | grep date.timezone # ; http://php.net/date.timezone # ;date.timezone = sed -i 's/;date.timezone =/date.timezone = Asia\/Shanghai/g' /etc/php/7.2/apache2/php.ini | grep date.timezone cat /etc/php/7.2/apache2/php.ini | grep date.timezone systemctl restart apache2 echo "========================> 优化配置:PHP - Recommendations (cli)" cat /etc/php/7.2/cli/php.ini | grep memory_limit # memory_limit = 128M sed -ri /^"memory_limit ="/c"memory_limit = 400M" /etc/php/7.2/cli/php.ini cat /etc/php/7.2/cli/php.ini | grep memory_limit # memory_limit = 400M cat /etc/php/7.2/cli/php.ini | grep max_execution_time sed -ri /^"max_execution_time ="/c"max_execution_time = 60" /etc/php/7.2/cli/php.ini cat /etc/php/7.2/cli/php.ini | grep max_execution_time # max_execution_time = 60 cat /etc/php/7.2/cli/php.ini | grep date.timezone # ; http://php.net/date.timezone # ;date.timezone = sed -i 's/;date.timezone =/date.timezone = Asia\/Shanghai/g' /etc/php/7.2/cli/php.ini | grep date.timezone cat /etc/php/7.2/cli/php.ini | grep date.timezone # date.timezone = Asia/Shanghai systemctl restart apache2 echo "========================> 优化配置:PHP - 模块支持 (必须)" # 还需要安装PHP模块支持 apt install php7.2-gd php7.2-gmp php7.2-ldap php7.2-mbstring php7.2-simplexml php7.2-xml -y systemctl restart apache2 echo "========================> 优化配置:PHP - 模块支持(可选)" apt install php7.2-snmp -y systemctl restart apache2 echo "========================> 优化配置:MySQL - 时区支持" # 您的Cacti 数据库登录帐户无权访问MySQL TimeZone 数据库. 请提供Cacti 数据库帐户对 "mysql" 数据库中 "time_zone_name" 表的 "select" 访问权,并在继续之前填充MySQL 的TimeZone 信息. mysql -uroot -p$MYSQL_ROOT_PASSWORD -e "GRANT SELECT ON mysql.time_zone_name TO cactiuser@localhost IDENTIFIED BY 'cactiuser'; flush privileges" # ERROR: 您的MySQL TimeZone 数据库未被填充. 请在继续之前填入此数据库. /usr/bin/mysql_tzinfo_to_sql /usr/share/zoneinfo/ | mysql -uroot -p$MYSQL_ROOT_PASSWORD mysql echo "========================> 优化配置:MySQL - 设置" apt install crudini -y # 当Cacti 用于非英语的其它语言环境时, 数据库使用utf8mb4_general_ci 排序规则非常重要,因为某些字符的值不止一个字节. crudini --get /etc/mysql/mariadb.conf.d/50-server.cnf mysqld collation-server # utf8mb4_general_ci crudini --set /etc/mysql/mariadb.conf.d/50-server.cnf mysqld collation-server utf8mb4_unicode_ci crudini --get /etc/mysql/mariadb.conf.d/50-server.cnf mysqld collation-server # utf8mb4_unicode_ci crudini --set /etc/mysql/mariadb.conf.d/50-server.cnf mysqld max_heap_table_size 31M # 执行具有较大临时表大小的子查询时,请将这些临时表保存在内存中. crudini --set /etc/mysql/mariadb.conf.d/50-server.cnf mysqld tmp_table_size 31M # 在执行连接时,如果它们低于此大小,它们将被保存在内存中,并且不会写入临时文件. crudini --set /etc/mysql/mariadb.conf.d/50-server.cnf mysqld join_buffer_size 62M crudini --set /etc/mysql/mariadb.conf.d/50-server.cnf mysqld innodb_file_format Barracuda crudini --set /etc/mysql/mariadb.conf.d/50-server.cnf mysqld innodb_large_prefix 1 crudini --set /etc/mysql/mariadb.conf.d/50-server.cnf mysqld innodb_buffer_pool_size 481M # 从 MariaDB 10.3.29-0+deb10u1开始,您可以控制 MariaDB将交易刷新到磁盘的频率. 默认值是1秒,但是在高I/O系统中设置为大于1的值可以允许磁盘I/O更有序 crudini --set /etc/mysql/mariadb.conf.d/50-server.cnf mysqld innodb_flush_log_at_timeout 3 # 采用现代SSD类型的存储器,具有多个读取IO线程对于具有高IO特性的应用是有利的. crudini --set /etc/mysql/mariadb.conf.d/50-server.cnf mysqld innodb_read_io_threads 32 # 采用现代SSD类型的存储器,具有多个写入IO线程对于具有高IO特性的应用是有利的. crudini --set /etc/mysql/mariadb.conf.d/50-server.cnf mysqld innodb_write_io_threads 16 #MariaDB会将innodb_buffer_pool分成内存区域以提高性能. 最大值是64.当您的innodb_buffer_pool小于1GB时,您应该使用池大小除以128MB. 继续使用这个等式,最大值为64. crudini --set /etc/mysql/mariadb.conf.d/50-server.cnf mysqld innodb_buffer_pool_instances 5 # 如果您有SSD磁盘,请使用此建议。如果您有物理硬盘驱动器,请使用200*阵列中活动驱动器的数量。如果使用NVMe或PCIe闪存,则可以使用高达100000的更大数字。 crudini --set /etc/mysql/mariadb.conf.d/50-server.cnf mysqld innodb_io_capacity 5000 #如果您有SSD磁盘,请使用此建议。如果您有物理硬盘驱动器,请使用2000*阵列中活动驱动器的数量。如果使用NVMe或PCIe闪存,则可以使用高达200000的更大数字。 crudini --set /etc/mysql/mariadb.conf.d/50-server.cnf mysqld innodb_io_capacity_max 10000 systemctl restart mysql echo "========================> 优化配置:目录权限检查" cd $CACTI_WORKDIR chown -R www-data:www-data rra/ log/ resource/ scripts/ include/ cache/ echo "========================> 优化配置:数据库排序" mysql -uroot -p$MYSQL_ROOT_PASSWORD -e "ALTER DATABASE cacti CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;" systemctl restart mysql } installCactiSpine(){ echo echo echo "========================> 安装cacti-spine" if [ -f /usr/local/spine/bin/spine ]; then eixt 0 fi cd $CACTI_PATH if [ ! -f $CACTI_SPINE_VERSION.tar.gz ]; then wget https://files.cacti.net/spine/$CACTI_SPINE_VERSION.tar.gz fi tar -zxf $CACTI_SPINE_VERSION.tar.gz -C $CACTI_PATH # 安装相关依赖 apt install build-essential dos2unix dh-autoreconf help2man libssl-dev librrds-perl libsnmp-dev libmariadb-dev libmariadbclient-dev -y cd $CACTI_PATH/$CACTI_SPINE_VERSION ./bootstrap ./configure make make install # 编辑Cacti Spine配置文件,并将Cacti数据库名称,用户名和密码添加到Spine conf文件中 cp /usr/local/spine/etc/spine.conf.dist /usr/local/spine/etc/spine.conf cat /usr/local/spine/etc/spine.conf | egrep "^DB_" # DB_Host localhost # DB_Database cacti # DB_User cactiuser # DB_Pass cactiuser # DB_Port 3306 } renewCollcetInterval(){ # 创建任务计划 echo "------> 修改数据采集周期为1分钟" tee /etc/cron.d/cacti << EOF */1 * * * * www-data /usr/bin/php $CACTI_WORKDIR/poller.php > /dev/null 2>&1 EOF systemctl restart cron.service echo "在 http://192.168.168.200/cacti/settings.php?tab=poller& 设置---Poller中修改 poller 周期、Cron/Daemon Interval 为每分钟" } upgradeCacti(){ echo "------> 备份Cacti数据库" mysqldump -uroot -p$MYSQL_ROOT_PASSWORD -l --add-drop-table --lock-tables=false cacti > $CACTI_PATH/mysql.cacti echo "------> 备份旧的 Cacti 目录,但 RRDfiles 除外" tar --exclude=*.rrd -zcvf $CACTI_PATH/${CACTI_VERSION}_backup_`date "+%Y%m%d"`.tgz $CACTI_WORKDIR echo "------> 下载新版 Cacti 解压" cd $CACTI_PATH if [ ! -f $CACTI_VERSION_NEW.tar.gz ];then wget https://files.cacti.net/cacti/linux/$CACTI_VERSION_NEW.tar.gz fi tar -zxf $CACTI_VERSION_NEW.tar.gz -C $CACTI_PATH echo "------> 覆盖cacti文件" cp -rpf $CACTI_PATH/$CACTI_VERSION_NEW/* $CACTI_WORKDIR echo "------> 进入目录,并设置适当的权限" cd $CACTI_WORKDIR chown -R www-data:www-data rra/ log/ resource/ scripts/ include/ cache/ systemctl restart apache2 systemctl restart cron.service systemctl restart php7.2-fpm.service } installCacti(){ installApache installMySQL installPHP installTools installCactiPkg configConnMySQL # 从 Cacti 1.2.16 开始,可以选择使用旧的 Crontab 方式或可选的 cactid 服务(不能启动,未知原因)来运行 Cacti 轮询器。所以下面二选一 configCronJob # configSystemd optimizedConfig installCactiSpine echo echo echo "安装完成,浏览器访问 http://ip/cacti 进行配置" echo "配置完成,在 http://ip/cacti/settings.php?tab=path& 系统设置-设置-路径 修改Spine 配置文件路径:/usr/local/spine/etc/spine.conf" } # 新安装 installCacti # 版本升级:1.2.17->1.2.18 # upgradeCacti ```

很赞哦! (0)

文章交流

  • emoji
0人参与,0条评论

当前用户

未登录,点击   登录

站点信息

  • 建站时间:网站已运行2285天
  • 系统信息:Linux
  • 后台程序:Python: 3.8.10
  • 网站框架:Django: 3.2.6
  • 文章统计:257 篇
  • 文章评论:63 条
  • 腾讯分析网站概况-腾讯分析
  • 百度统计网站概况-百度统计
  • 公众号:微信扫描二维码,关注我们
  • QQ群:QQ加群,下载网站的学习源码
返回
顶部
标题 换行 登录
网站