Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Automated Installation and Configuration of Cacti 1.2.16 on CentOS 7

Tech 1

Deploying Cacti requires specific system prerequisites to ensure stability and performance. The target environment must run CentOS 7 or higher with stable network connectivity. PHP version 7.x or later is required, along with a database backend such as MariaDB or MySQL version 5.7 or newer.

Deployment Script Overview

The following automation script handles dependency installation, database configuration, web server setup, and Cacti core initialization. It disables SELinux, configures repositories, compiles RRDtool, sets up Spine, and adjusts PHP and MariaDB parameters for optimal performance.

Execution Instructions

Save the script below as deploy_cacti.sh in the root directory. Ensure execution permissions are granted before running.

chmod +x deploy_cacti.sh
./deploy_cacti.sh

Automation Script

#!/bin/bash
set -euo pipefail

# Configuration Variables
CACTI_VER="1.2.16"
RRD_VER="1.7.2"
WORKSPACE=$(pwd)
DB_NAME="cacti_monitor"
DB_USER="cacti_svc"
DB_PASS="CactiStrongPwd2023"
ROOT_PASS="CactiStrongPwd2023"

# Utility Functions
log_info() {
    echo "[INFO] $1"
}

log_error() {
    echo "[ERROR] $1" >&2
    exit 1
}

# Privilege Check
verify_privileges() {
    if [[ $EUID -ne 0 ]]; then
        log_error "This script must be executed as root."
    fi
}

# Security Configuration
configure_selinux() {
    log_info "Disabling SELinux..."
    setenforce 0
    sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
}

# Repository Setup
setup_mariadb_repository() {
    log_info "Configuring MariaDB repository..."
    cat > /etc/yum.repos.d/MariaDB.repo <<EOF
[mariadb]
name = MariaDB
baseurl = https://mirrors.ustc.edu.cn/mariadb/yum/10.3/centos7-amd64
gpgkey = https://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck = 1
EOF
    yum clean all
    yum makecache
}

# Dependency Installation
install_dependencies() {
    log_info "Installing system dependencies..."
    yum -y install gcc make automake httpd php php-mysqlnd php-snmp php-xml php-gd php-ldap php-mbstring php-posix \
        mariadb-client mariadb-server mariadb-devel mariadb-compat mariadb-common pcre-devel pango pango-devel \
        cairo-devel libxml2-devel pixman-devel perl-devel fontconfig-devel freetype libpng-devel binutils cpp \
        net-snmp net-snmp-utils net-snmp-devel openssl-devel autoconf dos2unix libtool glibc-devel wget patch \
        fontconfig ttmkfdir
}

# RRDtool Compilation
compile_rrdtool() {
    log_info "Compiling RRDtool..."
    tar -xzvf rrdtool-${RRD_VER}.tar.gz
    pushd rrdtool-${RRD_VER}
    sed -i 's/RRDTOOL / TOBI OETIKER\/AUTOMATED BUILD/' src/rrd_graph.c
    ./configure
    make && make install
    popd
    cp /opt/rrdtool-${RRD_VER}/bin/* /usr/bin/
}

# SNMP Service
enable_snmp() {
    log_info "Enabling SNMP daemon..."
    systemctl enable --now snmpd
}

# Spine Installation
install_spine() {
    log_info "Installing Spine poller..."
    tar zxvf cacti-spine-${CACTI_VER}.tar.gz
    pushd cacti-spine-${CACTI_VER}
    ./bootstrap
    ./configure
    make
    cp spine /usr/bin/spine
    cp spine.conf.dist /etc/spine.conf
    popd
    chown apache:apache /etc/spine.conf
    sed -i "s/^DB_Pass.*/DB_Pass    ${DB_PASS}/" /etc/spine.conf
}

# Database Initialization
initialize_database() {
    log_info "Starting MariaDB and creating database..."
    systemctl enable --now mariadb
    
    mysqladmin -uroot password "${ROOT_PASS}"
    
    mysql -uroot -p"${ROOT_PASS}" -e "CREATE DATABASE ${DB_NAME};"
    mysql -uroot -p"${ROOT_PASS}" -e "GRANT ALL ON ${DB_NAME}.* TO '${DB_USER}'@'localhost' IDENTIFIED BY '${DB_PASS}';"
    mysql -uroot -p"${ROOT_PASS}" -e "FLUSH PRIVILEGES;"
    mysql -uroot -p"${ROOT_PASS}" -e "ALTER DATABASE ${DB_NAME} CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
}

# Cacti Core Installation
install_cacti_core() {
    log_info "Deploying Cacti files..."
    tar zxvf cacti-${CACTI_VER}.tar.gz
    mv cacti-${CACTI_VER} /var/www/html/cacti
    
    chmod -R a-w /var/www/html/cacti/
    
    # Set ownership for writable directories
    chown -R apache:apache /var/www/html/cacti/resource/snmp_queries/
    chown -R apache:apache /var/www/html/cacti/resource/script_server/
    chown -R apache:apache /var/www/html/cacti/resource/script_queries/
    chown -R apache:apache /var/www/html/cacti/scripts/
    chown -R apache:apache /var/www/html/cacti/cache/
    chown -R apache:apache /var/www/html/cacti/rra
    chown -R apache:apache /var/www/html/cacti/log
    
    # Set permissions
    chmod -R u+w /var/www/html/cacti/resource/
    chmod -R u+w /var/www/html/cacti/scripts/
    chmod -R u+w /var/www/html/cacti/cache/
    chmod -R 775 /var/www/html/cacti/rra
    chmod -R 775 /var/www/html/cacti/log
    
    # Import Schema
    mysql -u ${DB_USER} -p${DB_PASS} ${DB_NAME} < /var/www/html/cacti/cacti.sql
}

# Configuration Updates
configure_cacti_files() {
    log_info "Updating configuration files..."
    sed -i "s/^\$database_default.*/\$database_default = '${DB_NAME}';/" /var/www/html/cacti/include/config.php
    sed -i "s/^\$database_username.*/\$database_username = '${DB_USER}';/" /var/www/html/cacti/include/config.php
    sed -i "s/^\$database_password.*/\$database_password = '${DB_PASS}';/" /var/www/html/cacti/include/config.php
    
    # Set Timezone
    sed -i "24a date_default_timezone_set('Asia/Shanghai');" /var/www/html/cacti/include/global.php
}

# Cron Job Setup
setup_cron() {
    log_info "Configuring poller cron job..."
    echo "*/5 * * * * /usr/bin/php /var/www/html/cacti/poller.php >> /tmp/cacti_poller.log 2>&1" | crontab -
}

# PHP Tuning
tune_php() {
    log_info "Adjusting PHP settings..."
    sed -i 's/^memory_limit.*/memory_limit = 1536M/' /etc/php.ini
    sed -i 's/^max_execution_time.*/max_execution_time = 100/' /etc/php.ini
    sed -i 's/^; date.timezone.*/date.timezone = "Asia\/Shanghai"/' /etc/php.ini
}

# Database Tuning
tune_mariadb() {
    log_info "Optimizing MariaDB configuration..."
    cat >> /etc/my.cnf.d/server.cnf <<EOF
[mysqld]
character_set_server=utf8mb4
character_set_client=utf8mb4
collation_server=utf8mb4_unicode_ci
max_heap_table_size=768M
tmp_table_size=768M
join_buffer_size=1024M
innodb_file_format=Barracuda
innodb_large_prefix=1
innodb_buffer_pool_size=8192M
innodb_flush_log_at_timeout=3
innodb_read_io_threads=32
innodb_write_io_threads=16
innodb_buffer_pool_instances=64
innodb_io_capacity=400
innodb_io_capacity_max=4000
EOF
    
    # Load Timezones
    mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -uroot -p"${ROOT_PASS}" mysql
    mysql -uroot -p"${ROOT_PASS}" -e "GRANT SELECT ON mysql.time_zone_name TO '${DB_USER}'@'localhost';"
    
    systemctl restart mariadb
}

# Font Configuration
setup_fonts() {
    log_info "Configuring fonts..."
    mkdir -p /usr/share/fonts/chinese
    # Copy custom fonts here if available
    # cp ${WORKSPACE}/YOUR_FONT.TTF /usr/share/fonts/chinese/
    ttmkfdir -e /usr/share/X11/fonts/encodings/encodings.dir
    fc-cache -fv
}

# Plugin Installation
install_plugins() {
    log_info "Extracting plugins..."
    cd ${WORKSPACE}
    # Ensure plugin tarballs exist before extraction
    if [ -f "plugin_monitor-2.3.6.tar.gz" ]; then
        tar xzvf plugin_monitor-2.3.6.tar.gz
        mv plugin_monitor-2.3.6 /var/www/html/cacti/plugins/monitor
    fi
    if [ -f "plugin_thold-1.3.2.tar.gz" ]; then
        tar xzvf plugin_thold-1.3.2.tar.gz
        mv plugin_thold-develop /var/www/html/cacti/plugins/thold
    fi
}

# Web Server Setup
setup_httpd() {
    log_info "Starting Web Server..."
    systemctl enable --now httpd
}

# Firewall Configuration
configure_firewall() {
    log_info "Opening HTTP port..."
    firewall-cmd --permanent --add-service=http
    firewall-cmd --reload
}

# Main Execution Flow
main() {
    verify_privileges
    configure_selinux
    setup_mariadb_repository
    install_dependencies
    compile_rrdtool
    enable_snmp
    install_spine
    initialize_database
    install_cacti_core
    configure_cacti_files
    setup_cron
    tune_php
    tune_mariadb
    setup_fonts
    install_plugins
    setup_httpd
    configure_firewall
    
    log_info "Installation completed successfully."
}

main

Post-Installation Verification

After the script completes, access the Cacti web interface via http://<server-ip>/cacti. The initial setup wizard will guide you through the final configuration steps. Ensure the Spine poller is selected in the Cacti settings for improved performance over the default cmd.php poller. Monitor the /tmp/cacti_poller.log file to verify that data collection tasks are executing without errors.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.