Cacti部署(一)

一、环境准备

1.1.系统环境

cat /etc/centos-release
CentOS Linux release 7.4.1708 (Core)

1.2.更换国内yum源

使用脚本

1.3.安装docker和docker-compose

使用脚本

1.4.时间同步

使用脚本

二、Cacti部署

2.1.cacti原理介绍

cacti是使用php语言实现的一个流量监控软件,它通过snmp服务获取数据,然后使用rrdtool储存和更新数据,当用户需要查看数据的时候生成图表给用户,至于mysql只是配合php程序存储一些变量数据,比如主机名,ip,端口号等;snmp抓取的数据不是存储在mysql里面,而是存在rrdtool生成的rrd文件中,rrdtool对数据的更新就是对rrd文件进行处理

2.2.应用部署

2.2.1.编写nginx的Dockerfile

cat Dockerfile_nginx

FROM centos:7
MAINTAINER linuxwt <tengwanginit@gmail.com>

# 更换163源
RUN yum -y install wget
RUN cd /etc/yum.repos.d \
&&  mv CentOS-Base.repo CentOS-Base.repo.bak \
&& wget  http://mirrors.163.com/.help/CentOS7-Base-163.repo \
&& mv CentOS7-Base-163.repo CentOS-Base.repo
RUN yum makecache && yum -y update


# 配置相关的依赖
RUN yum -y install gcc gcc-c++
RUN yum -y install pcre*
RUN yum -y install zlib*
RUN yum -y install make

RUN yum -y install pango*
RUN yum -y install perl-devel
RUN yum -y install libxml2 libxml2-devel

# 将宿主机解压后的源码包添加进来,然后编译安装
ADD ./nginx-1.15.5 /tmp
RUN mkdir -p /usr/local/Nginx
RUN cd /tmp  && ./configure --prefix=/usr/local/Nginx --with-poll_module --with-http_stub_status_module && make && make install
RUN echo "daemon off;" >> /usr/local/Nginx/conf/nginx.conf

#RUN cd /tmp && wget https://oss.oetiker.ch/rrdtool/pub/rrdtool-1.7.0.tar.gz && tar  zvxf rrdtool-1.7.0.tar.gz
#WORKDIR /tmp/rrdtool-1.7.0
#RUN ./configure --prefix=/usr/local/rrdtool && make && make install
#RUN yum install -y net-snmp net-snmp-devel  net-snmp-libs  net-snmp-perl net-snmp-utils mrtg

expose 80

CMD ["/usr/local/Nginx/sbin/nginx"]  

同级目录注意需要有nginx解压包

2.2.2.编写php的Dockerfile

cat Dockerfile_php

FROM centos:7
MAINTAINER linuxwt <tengwanginit@gmail.com>

# 更换163源
RUN yum -y install wget
RUN cd /etc/yum.repos.d \
&&  mv CentOS-Base.repo CentOS-Base.repo.bak \
&& wget  http://mirrors.163.com/.help/CentOS7-Base-163.repo \ 
&& mv CentOS7-Base-163.repo CentOS-Base.repo
RUN yum makecache && yum -y update

# 安装依赖
RUN yum -y install gcc gcc-c++
RUN yum -y install make

ADD libmcrypt-2.5.7.tar.gz /tmp 
WORKDIR /tmp/libmcrypt-2.5.7
RUN ./configure && make && make install

ADD php-5.6.38.tar.gz /tmp
RUN yum -y install perl-devel ibxml2 libxml2-devel bzip2 bzip2-devel libjpeg-turbo libjpeg-turbo-devel libpng libpng-devel freetype freetype-devel zlib zlib-devel libcurl libcurl-devel  openssl openssl-devel openldap openldap-devel 


WORKDIR /tmp/php-5.6.38
RUN ./configure --prefix=/usr/local/php --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-mysql=mysqlnd --with-openssl --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-mcrypt --with-zlib --with-libxml-dir=/usr --enable-xml  --enable-sockets --enable-fpm --with-config-file-path=/usr/local/php/etc --with-bz2 --with-gd  --enable-gd-native-ttf  --enable-sockets --enable-mbstring --enable-bcmath --without-pear  --disable-phar && make && make install

RUN wget http://pear.php.net/go-pear.phar 
RUN /usr/local/php/bin/php go-pear.phar

RUN yum install pango* -y  && yum install perl-devel -y
RUN cd /tmp && wget https://oss.oetiker.ch/rrdtool/pub/rrdtool-1.7.0.tar.gz && tar zvxf rrdtool-1.7.0.tar.gz
WORKDIR /tmp/rrdtool-1.7.0
RUN ./configure --prefix=/usr/local/rrdtool && make && make install
RUN yum -y install  net-snmp-utils net-snmp-libs net-snmp net-snmp-devel
#RUN /usr/sbin/snmpd
#RUN yum -y install net-snmp net-snmp-devel
#RUN cd /tmp && wget http://ftp.gnu.org/gnu/m4/m4-1.4.9.tar.gz && tar zvxf m4-1.4.9.tar.gz && cd m4-1.4.9/ && ./configure && make && make install 
#RUN cd /tmp && wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.62.tar.gz && tar -zvxf autoconf-2.62.tar.gz && cd autoconf-2.62/ && ./configure && make && make install

RUN  yum -y install m4
RUN  yum -y install autoconf

RUN cp -frp /usr/lib64/libldap* /usr/lib && yum -y install gmp-devel && cd /tmp/php-5.6.38/ext/ldap && /usr/local/php/bin/phpize && ./configure --with-php-config=/usr/local/php/bin/php-config && make && make install

RUN cd /tmp/php-5.6.38/ext/snmp && /usr/local/php/bin/phpize && ./configure --with-php-config=/usr/local/php/bin/php-config && make && make install
RUN cd /tmp/php-5.6.38/ext/gmp && /usr/local/php/bin/phpize && ./configure --with-php-config=/usr/local/php/bin/php-config && make && make install


RUN cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf 

RUN useradd -M -s /sbin/nologin php
RUN sed -i -e 's@;pid = run/php-fpm.pid@pid = run/php-fpm.pid@g' -e 's@nobody@php@g' -e 's@listen = 127.0.0.1:9000@listen = 0.0.0.0:9000@g' /usr/local/php/etc/php-fpm.conf
RUN sed -i 's@;daemonize = yes@daemonize = no@g' /usr/local/php/etc/php-fpm.conf
EXPOSE 9000

CMD ["/usr/local/php/sbin/php-fpm"]

同级目录需要有php的压缩包和libmcrypt的压缩包

2.2.3.构建nginx镜像和php镜像

docker build -t nginx:cacti -< Dockerfile_nginx
docker build -t php:cacti -< Dockerfile_php
或者使用命令
docker build -t nginx:cacti -f Dockerfile_nginx .
docker build -t php:cacti -f Dockerfile_php .

2.2.4.编写docker-compose.yml文件

cat docker-compose.yml

nginx_cacti:
  restart: always
  image: nginx:cacti
  container_name: nginx_cacti
  volumes:
     - /etc/localtime:/etc/localtime
     - /etc/timezone:/etc/timezone
     - /usr/local/Nginx/html:/usr/local/Nginx/html
     - ./nginx.conf:/usr/local/Nginx/conf/nginx.conf
  privileged: true
  links:
     - php_cacti
  ports:
     - 80:80
php_cacti:
  restart: always
  image: php:cacti
  container_name: php_cacti
  volumes:
     - /etc/localtime:/etc/localtime
     - /etc/timezone:/etc/timezone
     - /usr/local/Nginx/html:/usr/local/Nginx/html
     - ./php-fpm.conf:/usr/local/php/etc/php-fpm.conf
     - ./php.ini:/usr/local/php/etc/php.ini
     - ./snmpd.conf:/etc/snmp/snmpd.conf
  privileged: true
  links:
     - mysql_cacti
  expose:
     - 9000
mysql_cacti:
  restart: always
  image: mysql:5.7
  container_name: mysql_cacti
  volumes:
    - /etc/localtime:/etc/localtime
    - /etc/timezone:/etc/timezone
    - $PWD/mysql:/var/lib/mysql
    - $PWD/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf
    - $PWD/mysql.log:/var/log/mysql/general.log
    - $PWD/err.log:/var/log/mysql/error.log
    - /usr/local/Nginx/html:/usr/local/Nginx/html
  ports:
    - 33066:3306
  environment:
    MYSQL_ROOT_PASSWORD: rootpassword

针对上面这个文件的某些挂载目录作一些说明

  • /usr/local/Nginx/html
    nginx容器起一个页面展示的作用,所以我们把cacti源代码放入nginx的根目录,而php需要去执行源代码里的二进制文件,mysql后面也需要导入cacti初始化数据库,所以这里的每一个容器里都对目录/usr/local/Nginx/html进行了挂载
  • php.ini
    php的配置文件,这里我们将其映射出来,方便修改配置
  • php-fpm.conf
    php-fpm的配置文件,本身php不会自动生成,在进行php镜像构建的时候我们通过复制php-fpm.conf.default生成,为了随时修改我们将其映射出来
  • snpmd.conf
    snmpd服务的配置文件,目的同上

2.2.5.nginx配置文件

cat nginx.conf

user  root;
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm index.php;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ \.php$ {
            root           html;
            fastcgi_pass   php_cacti:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/Nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}

2.2.6.php配置文件

cat php.ini

[PHP]
engine = On
short_open_tag = Off
asp_tags = Off
precision = 14
output_buffering = 4096
zlib.output_compression = Off
implicit_flush = Off
unserialize_callback_func =
serialize_precision = 17
disable_functions =
disable_classes =
zend.enable_gc = On
expose_php = On
max_execution_time = 30
max_input_time = 60
memory_limit = 128M
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
display_errors = Off
display_startup_errors = Off
log_errors = On
log_errors_max_len = 1024
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
track_errors = Off
html_errors = On
variables_order = "GPCS"
request_order = "GP"
register_argc_argv = Off
auto_globals_jit = On
post_max_size = 8M
auto_prepend_file =
auto_append_file =
default_mimetype = "text/html"
default_charset = "UTF-8"
doc_root =
user_dir =
enable_dl = Off
file_uploads = On
upload_max_filesize = 2M
max_file_uploads = 20
allow_url_fopen = On
allow_url_include = Off
default_socket_timeout = 60
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/ldap.so
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/snmp.so
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/gmp.so
[CLI Server]
cli_server.color = On
[Date]
date.timezone = "Asia/Shanghai"
[filter]
[iconv]
[intl]
[sqlite3]
[Pcre]
[Pdo]
[Pdo_mysql]
pdo_mysql.cache_size = 2000
pdo_mysql.default_socket=
[Phar]
[mail function]
SMTP = localhost
smtp_port = 25
mail.add_x_header = On
[SQL]
sql.safe_mode = Off
[ODBC]
odbc.allow_persistent = On
odbc.check_persistent = On
odbc.max_persistent = -1
odbc.max_links = -1
odbc.defaultlrl = 4096
odbc.defaultbinmode = 1
[Interbase]
ibase.allow_persistent = 1
ibase.max_persistent = -1
ibase.max_links = -1
ibase.timestampformat = "%Y-%m-%d %H:%M:%S"
ibase.dateformat = "%Y-%m-%d"
ibase.timeformat = "%H:%M:%S"
[MySQL]
mysql.allow_local_infile = On
mysql.allow_persistent = On
mysql.cache_size = 2000
mysql.max_persistent = -1
mysql.max_links = -1
mysql.default_port =
mysql.default_socket =
mysql.default_host =
mysql.default_user =
mysql.default_password =
mysql.connect_timeout = 60
mysql.trace_mode = Off
[MySQLi]
mysqli.max_persistent = -1
mysqli.allow_persistent = On
mysqli.max_links = -1
mysqli.cache_size = 2000
mysqli.default_port = 3306
mysqli.default_socket =
mysqli.default_host =
mysqli.default_user =
mysqli.default_pw =
mysqli.reconnect = Off
[mysqlnd]
mysqlnd.collect_statistics = On
mysqlnd.collect_memory_statistics = Off
[OCI8]
[PostgreSQL]
pgsql.allow_persistent = On
pgsql.auto_reset_persistent = Off
pgsql.max_persistent = -1
pgsql.max_links = -1
pgsql.ignore_notice = 0
pgsql.log_notice = 0
[Sybase-CT]
sybct.allow_persistent = On
sybct.max_persistent = -1
sybct.max_links = -1
sybct.min_server_severity = 10
sybct.min_client_severity = 10
[bcmath]
bcmath.scale = 0
[browscap]
[Session]
session.save_handler = files
session.use_strict_mode = 0
session.use_cookies = 1
session.use_only_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440
session.referer_check =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.hash_function = 0
session.hash_bits_per_character = 5
url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=fakeentry"
[MSSQL]
mssql.allow_persistent = On
mssql.max_persistent = -1
mssql.max_links = -1
mssql.min_error_severity = 10
mssql.min_message_severity = 10
mssql.compatibility_mode = Off
mssql.secure_connection = Off
[Assertion]
[COM]
[mbstring]
[gd]
[exif]
[Tidy]
tidy.clean_output = Off
[soap]
soap.wsdl_cache_enabled=1
soap.wsdl_cache_dir="/tmp"
soap.wsdl_cache_ttl=86400
soap.wsdl_cache_limit = 5
[sysvshm]
[ldap]
ldap.max_links = -1
[mcrypt]
[dba]
[opcache]
[curl]
[openssl]

cat php-fpm.conf

[global]
pid = run/php-fpm.pid
daemonize = no
 
 
[www]
user = php
group = php
listen = 0.0.0.0:9000
 
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

2.2.7.snmp配置文件

cat snmpd.conf

com2sec notConfigUser  172.168.1.101       public
group   notConfigGroup v1           notConfigUser
group   notConfigGroup v2c           notConfigUser
view    systemview    included   .1.3.6.1.2.1.1
view    systemview    included   .1.3.6.1.2.1.25.1.1
access  notConfigGroup ""      any       noauth    exact  systemview none none
view  all    included  .1                               80
syslocation Unknown (edit /etc/snmp/snmpd.conf)
syscontact Root <root@localhost> (configure /etc/snmp/snmp.local.conf)
dontLogTCPWrappersConnects yes

注意上面的172.168.1.101是宿主机IP,下面给出被监控主机的配置

com2sec notConfigUser  192.168.0.101      public
group   notConfigGroup v1           notConfigUser
group   notConfigGroup v2c           notConfigUser
view    systemview    included   .1.3.6.1.2.1.1
view    systemview    included   .1.3.6.1.2.1.25.1.1
access  notConfigGroup ""      any       noauth    exact  all none none
view all included .1 80
syslocation Unknown (edit /etc/snmp/snmpd.conf)
syscontact Root <root@localhost> (configure /etc/snmp/snmp.local.conf)
dontLogTCPWrappersConnects yes

2.2.8.mysql配置文件

cat mysqld.conf

[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
symbolic-links=0
character-set-server=utf8mb4
collation_server=utf8mb4_unicode_ci
max_heap_table_size=3122M
join_buffer_size=64M
innodb_doublewrite=OFF
innodb_flush_log_at_timeout=3
innodb_read_io_threads=32
innodb_write_io_threads=16
back_log=500
wait_timeout=1800
max_connections=3000
max_user_connections=800
innodb_thread_concurrency=40
default-storage-engine=innodb
key_buffer_size=400M
innodb_buffer_pool_size=15609M
innodb_log_file_size=256M
innodb_flush_method=O_DIRECT
innodb_log_buffer_size=20M
query_cache_size=40M
read_buffer_size=4M
sort_buffer_size=4M
read_rnd_buffer_size=8M
tmp_table_size=64M
thread_cache_size=64
max_allowed_packet=200M
server-id=1
log_bin=mysql-bin
general-log=1
general_log_file=/var/log/mysql/general.log
log-error=/var/log/mysql/error.log
#bind-address           = 127.0.0.1

2.2.9.cacti配置

docker-compose up -d先启动容器

  • cacti下载
    首先下载cacti的压缩包解压至nginx根目录下
    tar zvxf cacti-1.1.38.tar.gz -C /usr/local/Nginx/
    cd /usr/local/Nginx/
    mv cacti-1.1.38 cacti
  • cacti数据库初始化
    docker exec -it mysql_cacti bash
    mysql -u root -prootpassword
    create database cacti;
    use cacti;
    source /usr/local/Nginx/html/cacti/cacti.sql
    create user 'cactiuser'@'%' identified by 'cactipassword';
    create user 'cactiuser'@'localhost' identified by 'cactipassword';
    grant all on *.* to root;
    grant all on cacti.* to root@localhost;
    grant all on cacti.* to cactiuser;
    grant all on cacti.* to cactiuser@localhost
    flush privileges;
  • 数据库连接设置
    vi /usr/local/Nginx/html/cacti/include/config.php
$database_type     = 'mysql';
$database_default  = 'cacti';
$database_hostname = 'mysql_cacti';
$database_username = 'cactiuser';
$database_password = 'cactipassword';
$database_port     = '3306';
$database_ssl      = false;

重启所有容器使配置生效
访问地址http://172.168.1.122/cacti就可以进入安装界面了,至此算是cacti部署成功的第一步

2.3.cacti安装与启动

2.3.1.cacti安装向导

如果上面的操作顺利就可以看到web的安装界面,按照提示进行操作最后进入下面的界面
11bu-huo
默认的账号密码为admin admin,第一次登陆会强制修改密码
进入后我们点击一下settings查看里面的paths选项,这里有cacti使用的核心文件路径
11bu-huo-2
如果上面路径有不对的需要修正

2.3.2.cacti启动

部署成功后,需要后台开启snmpd服务并启动轮询器,同时每五分钟执行一次轮询
容器内 nohup /usr/sbin/snmpd -LS0-6d -f &
容器外 crontab -e
*/5 * * * * docker exec php_cacti /usr/local/php/bin/php /usr/local/Nginx/html/cacti/poller.php --force
systemctl restart crond.service
我们可以手动在容器内执行一次该命令使poller.php开始运作,至此cacti就算是部署成功了。

2.4.cacti部署层面的优化

2.4.1.更换轮询器为spine

cacti默认使用的采集器为cmd.php,最低也要五分钟收集一次,spine最低可以到1分钟采集一次,为了更高效的采集数据,我们安装spine,修改php的Dockerfile,如下所示
在原dockerfile的此位置

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

后面添加

RUN yum -y install  net-snmp-utils net-snmp-libs net-snmp net-snmp-devel mysql-devel mysql help2man 
ADD cacti-spine-1.1.38.tar.gz /tmp
WORKDIR /tmp/cacti-spine-1.1.38
RUN ./configure --prefix=/usr/local/spine && make && make install
RUN cp /usr/local/spine/etc/spine.conf.dist /usr/local/spine/etc/spine.conf 
RUN cp /usr/local/spine/etc/spine.conf /etc/spine.conf

注意同级目录要有spine压缩包
然后我们进入web界面设置spine的路径,如下图所示
11000
同时需要把spine.conf文件映射出来,方便我们修改配置
cat spine.conf

DB_Host                 mysql_cacti
DB_Database             cacti
DB_User                 cactiuser
DB_Pass                 cactipassword
DB_Port                 3306
RDB_Host                localhost
RDB_Database            cacti
RDB_User                cactiuser
RDB_Pass                cactiuser
RDB_Port                3306

在docker-compose.yml里的这里

- ./snmpd.conf:/etc/snmp/snmpd.conf

添加一行
- ./spine.conf:/etc/spine.conf
然后手动在容器内部启动spine
/usr/local/spine/bin/spine
然后我们可以修改前面的采集命令的计划任务时间
*/1 * * * * docker exec php_cacti /usr/local/php/bin/php /usr/local/Nginx/html/cacti/poller.php --force
但是我们还是需要去容器里面手动开始采集,这样我们就算是完成了由cmd.php采集向spine转变的过程

2.4.2.supervisor管理多进程

前面在构建php镜像的时候只有一个CMD命令,因为Dockerfile中只能运行一个CMD命令,所以每次重启应用我们都需要进容器内部手动启动snmpd服务,如果我们使用spine的话还要手动启动spine,那么如何才可以在启动容器的时候让它们自动启动呢,这就用到了supervisor这个进程管理器。
下面给出包含了supervisor、spine的php的dockerfile
cat dockerfile_php_supervisor

FROM centos:7
MAINTAINER linuxwt <tengwanginit@gmail.com>

# 更换163源
RUN yum -y install wget
RUN cd /etc/yum.repos.d \
&&  mv CentOS-Base.repo CentOS-Base.repo.bak \
&& wget  http://mirrors.163.com/.help/CentOS7-Base-163.repo \ 
&& mv CentOS7-Base-163.repo CentOS-Base.repo
RUN yum makecache && yum -y update

# 安装依赖
RUN yum -y install gcc gcc-c++
RUN yum -y install make
RUN yum -y install epel-release
RUN yum -y install supervisor
RUN mkdir -p /var/log/supervisor
COPY supervisord.conf /etc/supervisord.conf

ADD libmcrypt-2.5.7.tar.gz /tmp 
WORKDIR /tmp/libmcrypt-2.5.7
RUN ./configure && make && make install

ADD php-5.6.38.tar.gz /tmp
RUN yum -y install perl-devel ibxml2 libxml2-devel bzip2 bzip2-devel libjpeg-turbo libjpeg-turbo-devel libpng libpng-devel freetype freetype-devel zlib zlib-devel libcurl libcurl-devel  openssl openssl-devel openldap openldap-devel 


WORKDIR /tmp/php-5.6.38
RUN ./configure --prefix=/usr/local/php --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-mysql=mysqlnd --with-openssl --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-mcrypt --with-zlib --with-libxml-dir=/usr --enable-xml  --enable-sockets --enable-fpm --with-config-file-path=/usr/local/php/etc --with-bz2 --with-gd  --enable-gd-native-ttf  --enable-sockets --enable-mbstring --enable-bcmath --without-pear  --disable-phar && make && make install

RUN wget http://pear.php.net/go-pear.phar 
RUN /usr/local/php/bin/php go-pear.phar

RUN yum install pango* -y  && yum install perl-devel -y
RUN cd /tmp && wget https://oss.oetiker.ch/rrdtool/pub/rrdtool-1.7.0.tar.gz && tar zvxf rrdtool-1.7.0.tar.gz
WORKDIR /tmp/rrdtool-1.7.0
RUN ./configure --prefix=/usr/local/rrdtool && make && make install
RUN yum -y install  net-snmp-utils net-snmp-libs net-snmp net-snmp-devel mysql-devel mysql help2man 
ADD cacti-spine-1.1.38.tar.gz /tmp
WORKDIR /tmp/cacti-spine-1.1.38
RUN ./configure --prefix=/usr/local/spine && make && make install
RUN cp /usr/local/spine/etc/spine.conf.dist /usr/local/spine/etc/spine.conf 
RUN cp /usr/local/spine/etc/spine.conf /etc/spine.conf 
#RUN /usr/sbin/snmpd
#RUN yum -y install net-snmp net-snmp-devel
#RUN cd /tmp && wget http://ftp.gnu.org/gnu/m4/m4-1.4.9.tar.gz && tar zvxf m4-1.4.9.tar.gz && cd m4-1.4.9/ && ./configure && make && make install 
#RUN cd /tmp && wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.62.tar.gz && tar -zvxf autoconf-2.62.tar.gz && cd autoconf-2.62/ && ./configure && make && make install

RUN  yum -y install m4
RUN  yum -y install autoconf

RUN cp -frp /usr/lib64/libldap* /usr/lib && yum -y install gmp-devel && cd /tmp/php-5.6.38/ext/ldap && /usr/local/php/bin/phpize && ./configure --with-php-config=/usr/local/php/bin/php-config && make && make install

RUN cd /tmp/php-5.6.38/ext/snmp && /usr/local/php/bin/phpize && ./configure --with-php-config=/usr/local/php/bin/php-config && make && make install
RUN cd /tmp/php-5.6.38/ext/gmp && /usr/local/php/bin/phpize && ./configure --with-php-config=/usr/local/php/bin/php-config && make && make install


RUN cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf 

RUN useradd -M -s /sbin/nologin php
RUN sed -i -e 's@;pid = run/php-fpm.pid@pid = run/php-fpm.pid@g' -e 's@nobody@php@g' -e 's@listen = 127.0.0.1:9000@listen = 0.0.0.0:9000@g' /usr/local/php/etc/php-fpm.conf
RUN sed -i 's@;daemonize = yes@daemonize = no@g' /usr/local/php/etc/php-fpm.conf
EXPOSE 9000

#CMD ["/usr/local/php/sbin/php-fpm"]
CMD ["/usr/bin/supervisord"]

上面这个Dockerfile除了包含spine的安装,还包含了supervisor的安装,注意,同级目录需要有supervisord.conf
cat supervisord.conf

[supervisord]
nodaemon=true

[program:php-fpm]
command=/usr/local/php/sbin/php-fpm

[program:snmpd]
command=/usr/sbin/snmpd -LS0-6d -f

[program:spine]
command=/usr/local/spine/bin/spine

这样就可以在使用新的php镜像启动容器的时候将spine、snmpd、php-fpm服务同时启动

2.5.问题

2.5.1.问题一

访问http://ip/cacti报错“FATAL: Connection to Cacti database failed. Please ensure the database is running and your credentials in config.php are valid”
在配置cacti数据库连接的时候我们需要的使cactiuser@'%'这个账户,但是为了方便,我们也赋予了cactiuser@localhost用户对cacti数据库的所有权限,且设定的密码与远程账户一样

2.5.2.问题二

安装cacti的时候报错“Your Cacti database login account does not have access to the MySQL TimeZone database. Please provide the Cacti database account "select" access to the "time_zone_name" table in the "mysql" database, and populate MySQL's TimeZone information before proceeding”
解决办法:grant select on mysql.time_zone_name to cactiuser@'%';

2.5.3.问题三

注意给生成localtime和timezone文件,不然docker会报错
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
echo "Asia/Shanghai" > /etc/timezone

2.5.4.问题四

如果出来的图像有乱码,请在php的Dockerfile里执行命令
yum -y install font*

2.5.5.问题五

我们默认的访问地址是 http://ip/cacti,
如果我们把系统搭建在局域网,现在想代理出来在外面也可以访问,比如想用ngrok.xxx.com:xxx,是行不通的,我们必须要要能直接用域名访问,不要带cacti这个二级目录才可以,需要修改两个地方:
1、cat /usr/local/Nginx/html/cacti/include/config.php
将$url_path = '/cacti/';修改为$url_path = '';
2、cat nginx.conf
修改根目录为/usr/local/Nginx/html/cacti,同时修改/usr/local/Nginx/html$fastcgi_script_name;为/usr/local/Nginx/html/cacti$fastcgi_script_name;

2.5.6.问题六

ERROR: Your MySQL TimeZone database is not populated. Please populate this database before proceeding.

/usr/bin/mysql_tzinfo_to_sql /usr/share/zoneinfo/ | mysql -u root -p mysql

2.5.7.问题七

启动相关容器后,配置好路径,还一定要注意把General里的RRDTOOL版本选对,要不然在rra目录下可能不回生成rrd文件,最后本文配置的snmpd.conf是指放在php_cacti容器里面的,里面的ip写宿主机的ip,同时被监控的主机上的snmpd.conf里也写宿主机的ip,这里的宿主机是指cacti服务器,注意我们在写计划任务的时候最好是在手动开启采集器后来进行
配置snmp服务后,可以使用命令snmpwalk -v1 被监控主机ip -c public system来测试是否配置成功

三、链接

nginx-1.15.5下载
php-5.6.38下载
cacti-1.38下载
cacti-spine-1.38下载