第21周

1、搭建mogilefs

两台主机:centos6

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo

yum repolist

yum install -y epel-release

yum install perl-Net-Netmask perl-Sys-Syslog perl-IO-AIO perl-IO-String

mkdir -p /root/mogilefs

yum install /root/mogilefs/*.rpm                   ## 安装mogilefs    rpm包

mkdir -pv /data/mogdata/dev1                     ##第一台为dev1,第二台为dev2,只需要创建一个

mkdir -pv /data/mogdata/dev2

vim /etc/mogilefs/mogilefsd.conf

db_dsn = DBI:mysql:mogilefs:host=192.168.0.210

db_user = mogile 

db_pass = mogile 

listen = 0.0.0.0:7001

vim /etc/mogilefs/mogstored.conf

maxconns = 10000

httplisten = 0.0.0.0:7500

mgmtlisten = 0.0.0.0:7501

docroot = /data/mogdata

第三台安装数据库:

service mysql start

mysql -uroot -p123456

GRANT ALL ON *.* TO 'root'@'192.168.%.%' IDENTIFIED BY '123456';

grant all privileges on *.* to 'mogile'@'192.168.%.%' identified by 'mogile' with grant option;

FLUSH PRIVILEGES;

再回到前面一台主机上:

mogdbsetup --dbhost=192.168.0.210 --dbname=mogilefs --dbuser=mogile --dbpass=mogile --dbrootpass=123456 --yes

在数据库主机上查看是否成功创建数据库:

use mogilefs

show tables;

再回到前面的主机上:

chown -R mogilefs.mogilefs /data/mogdata/dev1     ##第一台为dev1,第二台为dev2

chown -R mogilefs.mogilefs /data/mogdata/dev2

chown -R mogilefs.mogilefs /var/run/mogilefsd/

service mogilefsd start

service mogstored start            ##启动报错,但是端口可以起来监听。可以用:mogstored -c /etc/mogilefs/mogstored.conf -daemon启动

mogadm --trackers=192.168.0.117:7001 host add 192.168.0.117 --ip=192.168.0.117 --status=alive                     ##添加主机

mogadm --trackers=192.168.0.117:7001 host add 192.168.0.118 --ip=192.168.0.118 --status=alive

mogadm --trackers=192.168.0.117:7001 host list

mogadm --trackers=192.168.0.117:7001 device add 192.168.0.117 1                                   ##添加设备

mogadm --trackers=192.168.0.117:7001 device add 192.168.0.118 2

mogadm --trackers=192.168.0.117:7001 device list

mogadm --trackers=192.168.0.117:7001 domain add images                                              ##添加域

mogadm --trackers=192.168.0.117:7001 domain add files

mogadm --trackers=192.168.0.117:7001 domain add html

mogadm --trackers=192.168.0.117:7001 domain list

mogadm --trackers=192.168.0.117:7001 class add images jpg                                            ##添加类

mogadm --trackers=192.168.0.117:7001 class add images gif

mogadm --trackers=192.168.0.117:7001 class add images pnf

mogadm --trackers=192.168.0.117:7001 class add images png

mogadm --trackers=192.168.0.117:7001 class add images jk1 --mindevcount=1               ##备份图像数量,指定jk1为1份       

mogadm --trackers=192.168.0.117:7001 class list

mogupload --trackers=192.168.0.117:7001 --domain=images --key='1.gif' --file=/var/www/icons/a.gif      ##上传文件,key后直接文件名,不能如/1.gif,不然nginx反代时会不成功

mogupload --trackers=192.168.0.117:7001 --domain=files --key='1.fstab.txt' --file=/etc/fstab

mogfileinfo --trackers=192.168.0.117:7001 --domain=files --key='1.fstab.txt'                    ##查看上传文件的信息

mogfileinfo --trackers=192.168.0.117:7001 --domain=files --key='1.gif'

然后在浏览器输入文件地址即可查看:

http://192.168.0.117:7500/dev1/0/000/000/0000000014.fid

2、用nginx反代mogilefs

借用上体环境,并搭建nginx服务器:

yum -y groupinstall "Development Tools" "Server Platform Development"

yum -y install openssl-devel pcre-devel

groupadd -r nginx

useradd -r -g nginx nginx

tar xf nginx-1.4.7.tar.gz

tar xf nginx_mogilefs_module-1.0.4

cd nginx-1.4.7

./configure \

  --prefix=/usr \

  --sbin-path=/usr/sbin/nginx \

  --conf-path=/etc/nginx/nginx.conf \

  --error-log-path=/var/log/nginx/error.log \

  --http-log-path=/var/log/nginx/access.log \

  --pid-path=/var/run/nginx/nginx.pid  \

  --lock-path=/var/lock/nginx.lock \

  --user=nginx \

  --group=nginx \

  --with-http_ssl_module \

  --with-http_flv_module \

  --with-http_stub_status_module \

  --with-http_gzip_static_module \

  --http-client-body-temp-path=/var/tmp/nginx/client/ \

  --http-proxy-temp-path=/var/tmp/nginx/proxy/ \

  --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \

  --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \

  --http-scgi-temp-path=/var/tmp/nginx/scgi \

  --with-pcre \

  --with-debug \

  --add-module=/root/桌面/nginx_mogilefs_module-1.0.4

make && make install

vim /etc/rc.d/init.d/nginx                     ##服务脚本

#!/bin/sh

#

# nginx - this script starts and stops the nginx daemon

#

# chkconfig:  - 85 15

# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \

#              proxy and IMAP/POP3 proxy server

# processname: nginx

# config:      /etc/nginx/nginx.conf

# config:      /etc/sysconfig/nginx

# pidfile:    /var/run/nginx.pid

# Source function library.

. /etc/rc.d/init.d/functions

# Source networking configuration.

. /etc/sysconfig/network

# Check that networking is up.

[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/sbin/nginx"

prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

make_dirs() {

  # make required directories

  user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`

  options=`$nginx -V 2>&1 | grep 'configure arguments:'`

  for opt in $options; do

      if [ `echo $opt | grep '.*-temp-path'` ]; then

          value=`echo $opt | cut -d "=" -f 2`

          if [ ! -d "$value" ]; then

              # echo "creating" $value

              mkdir -p $value && chown -R $user $value

          fi

      fi

  done

}

start() {

  [ -x $nginx ] || exit 5

  [ -f $NGINX_CONF_FILE ] || exit 6

  make_dirs

  echo -n $"Starting $prog: "

  daemon $nginx -c $NGINX_CONF_FILE

  retval=$?

  echo

  [ $retval -eq 0 ] && touch $lockfile

  return $retval

}

stop() {

  echo -n $"Stopping $prog: "

  killproc $prog -QUIT

  retval=$?

  echo

  [ $retval -eq 0 ] && rm -f $lockfile

  return $retval

}

restart() {

  configtest || return $?

  stop

  sleep 1

  start

}

reload() {

  configtest || return $?

  echo -n $"Reloading $prog: "

  killproc $nginx -HUP

  RETVAL=$?

  echo

}

force_reload() {

  restart

}

configtest() {

$nginx -t -c $NGINX_CONF_FILE

}

rh_status() {

  status $prog

}

rh_status_q() {

  rh_status >/dev/null 2>&1

}

case "$1" in

  start)

      rh_status_q && exit 0

      $1

      ;;

  stop)

      rh_status_q || exit 0

      $1

      ;;

  restart|configtest)

      $1

      ;;

  reload)

      rh_status_q || exit 7

      $1

      ;;

  force-reload)

      force_reload

      ;;

  status)

      rh_status

      ;;

  condrestart|try-restart)

      rh_status_q || exit 0

          ;;

  *)

      echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"

      exit 2

esac

chmod +x /etc/rc.d/init.d/nginx

chkconfig --add nginx

chkconfig nginx on

vim /etc/profile.d/nginx.sh

export PATH=/usr/sbin/nginx:$PATH

. /etc/profile.d/nginx.sh

service nginx start

vim /etc/nginx/nginx.conf

upstream mogtrackers {

      server 192.168.0.117:7001;

      server 192.168.0.118:7001;

    }

server {

        listen      80;

        server_name  192.168.0.116;

        location / {

            root  html;

            index  index.html index.htm;

        }

        location /images/ {

                mogilefs_tracker mogtrackers;

                mogilefs_domain images;

                mogilefs_class default;

                mogilefs_pass {

                        proxy_pass $mogilefs_path;

                        proxy_hide_header Content-Type;

                        proxy_buffering off;

                }

        }

        location /files/ {

                mogilefs_tracker mogtrackers;

                mogilefs_domain files;

                mogilefs_class default;

                mogilefs_pass  {

                        proxy_pass $mogilefs_path;

                        proxy_hide_header Content-Type;

                        proxy_buffering off;

                }

        }

}

nginx -s reload

在浏览器输入:

http://192.168.0.116/files/1.fstab.txt


3、搭建fastdfs

unzip libfastcommon-master.zip

unzip fastdfs-master.zip

unzip fastdfs-nginx-module-master.zip

cd libfastcommon-master/

./make.sh

./make.sh install

cd ../fastdfs-master/

./make.sh

./make.sh install

cp -r conf/* /etc/fdfs/

mkdir -p /data/fastdfs

cd /etc/fdfs/

vim clinet.conf

base_path=/data/fastdfs

tracker_server=10.105.104.138:22122

vim tracker.conf

base_path=/data/fastdfs

vim storage.conf

base_path=/data/fastdfs

store_path0=/data/fastdfs/storage

tracker_server=10.105.104.138:22122

mkdir -p /data/fastdfs/storage

/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start

/usr/bin/fdfs_storaged /etc/fdfs/storage.conf start

fdfs_test /etc/fdfs/client.conf upload /usr/share/httpd/icons/a.png      ##测试上传

安装nginx:

./configure

--prefix=/usr/local/nginx

--pid-path=/var/run/nginx/nginx.pid

--lock-path=/var/lock/nginx.lock

--error-log-path=/var/log/nginx/error.log

--http-log-path=/var/log/nginx/access.log

--with-http_gzip_static_module

--http-client-body-temp-path=/var/temp/nginx/client

--http-proxy-temp-path=/var/temp/nginx/proxy

--http-fastcgi-temp-path=/var/temp/nginx/fastcgi

--http-uwsgi-temp-path=/var/temp/nginx/uwsgi

--http-scgi-temp-path=/var/temp/nginx/scgi

--add-module=/home/admin/下载/fastdfs-nginx-module-master/src/

make && make install

mkdir -p /var/temp/nginx

cp /home/admin/fastdfs-nginx-module-master/src/mod_fastdfs.conf /etc/fdfs

vim /etc/fdfs/mod_fastdfs.conf

base_path=/data/fastdfs

store_path0=/data/fastdfs/storage

tracker_server=10.105.104.138:22122

url_have_group_name = true

vim /usr/local/nginx/conf/nginx.conf

server {

        listen      80;

        server_name  10.105.104.138;

        }

        location /group1/M00/ {

            root  /data/fastdfs/data;

            index  index.html index.htm;

            ngx_fastdfs_module;

        }

ln -s /data/fastdfs/data /data/fastdfs/data/M00

/usr/local/nginx/sbin/nginx

/usr/local/nginx/sbin/nginx -s reload

http://10.105.104.138/group1/M00/00/00/Cmloil3ksgGAAONHAAABMlGWAmw951.png                         ##通过浏览器查看


4、搭建zabbix监控服务

搭建zabbix服务主机:

yum install php-ldap httpd php-fpm php php-mysql zabbix40-server  zabbix40-web zabbix40-agent mariadb-server

vim /etc/my.cnf

innodb_file_per_table=ON

skip_name_resolve=ON

vim /etc/httpd/conf.d/zabbix.conf

php_value date.timezone Asia/Shanghai

systemctl start httpd mariadb zabbix-server zabbix-agent

mysql

create database zabbix charset 'utf8';

grant all on zabbix.* to 'zabbix'@'192.168.%.%' identified by '12345678';

grant all on zabbix.* to 'zabbix'@'127.0.0.1' identified by '12345678';

flush privileges;

gzip -d create.sql.gz                ## zabbix40-server 没有此文件

vim create.sql

USE zabbix;                  ## 在第一行加上

cat ./create.sql | mysql -uzabbix -p12345678 -h127.0.0.1

vim /etc/zabbix/zabbix_server.conf

DBHost=192.168.0.138

DBUser=zabbix

DBPassword=12345678

DBPort=3306

ListenIP=192.168.0.0/24

ListenIP=127.0.0.1

systemctl restart zabbix-server zabbix-agent httpd php-fpm

http://127.0.0.1/zabbix/               ##安装完毕后 默认账号密码:admin;zabbix

vim /etc/zabbix/zabbix_agentd.conf

EnableRemoteCommands=1

UnsafeUserParameters=1

在另外一台Linux主机搭建zabbix-agent

yum install zabbix-agent 

vim /etc/zabbix/zabbix_agentd.conf

Server=192.168.0.138                                 ##指向服务器IP 

ServerActive=192.168.0.138                       ##指向服务器IP

UnsafeUserParameters=1

systemctl start zabbix-agent 

回到zabbix服务器,设置参数并查看效果,创建Linux主机:

选择对应模板:

添加完成后,可用性 ZBX 为绿色即添加成功

采集数据后情况:

5、添加memory监控项,并出图

借用上题环境,默认模板里有memory监控项,也可以根据需要自行添加监控项:


6、实现报警

借用前面的环境:

在用户界面设置邮件收件地址:

在报警媒介类型设置脚本触发:

设置监控项,建立触发器:

设置报警动作,发送的用户:

设立编写报警脚本:

cat  /etc/zabbix/zabbix_server.conf         ##查看报警脚本路径

AlertScriptsPath=/var/lib/zabbixsrv/alertscripts

vim /var/lib/zabbixsrv/alertscripts/mail.py                      ##编写报警脚本

#!/usr/bin/python

#coding: utf-8

import smtplib

import sys

from email.mime.text import MIMEText

_user = "邮件发送用户"

_pwd  = "邮件密码"

_to  = "收件地址"

def send_mail(to,subject,contain):

    msg = MIMEText(contain)

    msg["Subject"] = subject

    msg["From"]    = _user

    msg["To"]      = to

    try:

        s = smtplib.SMTP_SSL("smtp.sina.com", 465)

        s.login(_user, _pwd)

        s.sendmail(_user, to, msg.as_string())

        s.quit()

        with open('/tmp/zabbix.log', 'w') as f:

            f.write("%s\n%s\n%s\n"%(to,subject,contain))

#        print "Success!"

    except smtplib.SMTPException,e:

        print "Falied,%s"%e

if __name__ == "__main__":

    send_mail(sys.argv[1], sys.argv[2], sys.argv[3])

chmod 755 /var/lib/zabbixsrv/alertscripts/mail.py

systemctl restart zabbix-server

python /var/lib/zabbixsrv/alertscripts/mail.py [email protected] nihao nihao                       ##测试脚本能否成功运行

满足触发器的触发条件,使之能够开始报警:

查看收件是否成功:

你可能感兴趣的:(第21周)