使用sed或awk显示指定行内容

文件内容为

[root@test1 test]# cat file.test
1
2
3
4
5
6
7
8
9
10

1. 显示第二行内容(指定行)

  • sed
[root@test1 test]# sed -n '2p' file.test
2
  • awk
[root@test1 test]# awk 'NR==2 {print $0}' file.test
2
[root@test1 test]# awk '{if(NR==2)print $0}' file.test
2

2. 显示第三行至第五行内容(指定行范围)

  • sed
[root@test1 test]# sed -n '3,5p' file.test
3
4
5
  • awk
[root@test1 test]# awk '{if(NR>2&&NR<6) print $0}' file.test
3
4
5
  • grep
[root@test1 test]# grep -C 1 4 file.test
3
4
5

3. 显示奇数行与偶数行

  • sed
[root@test1 test]# sed -n '1~2p' file.test
1
3
5
7
9
[root@test1 test]# sed -n '2~2p' file.test
2
4
6
8
10
[root@test1 test]# sed -n 'p;n' file.test
1
3
5
7
9
[root@test1 test]# sed -n 'n;p' file.test
2
4
6
8
10
  • awk
[root@test1 test]# awk 'NR%2==1' file.test
1
3
5
7
9
[root@test1 test]# awk 'NR%2==0' file.test
2
4
6
8
10
[root@test1 test]# awk '{if(NR%2==1) print $0}' file.test
1
3
5
7
9
[root@test1 test]#
[root@test1 test]# awk '{if(NR%2==0) print $0}' file.test
2
4
6
8
10

4. 显示匹配到的行

  • sed
[root@test1 test]# sed -n '/5/p' file.test
5 line 5
  • awk
[root@test1 test]# awk '/5/' file.test
5 line 5
  • grep
[root@test1 test]# grep 5 file.test
5 line 5

sed的12个练习题

1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#。

cp /etc/rc.d/rc.sysinit /tmp
sed -i 's/(^[[:space:]])/#1/g' /tmp/rc.sysinit

2、复制/boot/grub/grub.conf至/tmp目录中,删除/tmp/grub.conf文件中的行首的空白字符。

cp /boot/grub/grub.conf /tmp
sed -i 's/^[[:space:]]+//g' /tmp/grub.conf

3、删除/tmp/rc.sysinit文件中的以#开头,且后面跟了至少一个空白字符的行的#和空白字符。

sed -i 's@^#[[:space:]]+@@g' /tmp/rc.sysinit

4、为/tmp/grub.conf文件中前三行的行首加#号。

sed -i '1,3s@(^.)@#1@g' /tmp/grub.conf

5、将/etc/yum.repos.d/CentOS-Media.repo文件中所有的enabled=0或gpgcheck=0的最后的0修改为1

sed -i 's/enabled=0/enabled=1/g;s/gpgcheck=0/gpgcheck=1/g' /etc/yum.repos.d/CentOS-Media.repo

6、每4小时执行一次对/etc目录的备份,备份至/backup目录中,保存的目录名为形如etc-201504020202。

crontab -e
crontab -e 0*/4*** /etc /backup/etc-$(date +%Y%m%d%H%M)

7、每周2,4,6备份/var/log/messages文件至/backup/messages_logs/目录中,保存的文件名形如messages-20150402。

crontab -e
*00**2,4,6 cp -rf /var/log/messages /backup/message-$(date +%Y%m%d)

8、每天每两小时取当前系统/proc/meminfo文件中的所有以S开头的信息至/stats/memory.txt文件中。

crontab -e
1*/2**1,2,3,4,5 grep "^S" /proc/meminfo >> /stats/memry.txt

9、工作日的工作时间内,每两小时执行一次echo“howdy”。

crontab -e
1*/2**1,2,3,4,5 echo "howdy"

10、创建目录/tmp./testdir-当前日期时间

vim test1.sh
#!/bin/bash
#
mkdir /tmp/testdir-$(date +%Y%m%d)

11、在此目录创建100个空文件:file1-file100。

vim test2.sh
#!/bin/bash
#
for i in {1..100};do
touch /tmp/testdir-201609071346/file$i
done

12、显示/etc/passwd文件中位于第偶数行的用户的用户名。

vim test3.sh
#!/bin/bash
#
sed -n 'n;p' /etc/passwd | cut -d: -f1

Centos 7 Samba服务器搭建与用户配置

注:本文配置关闭了防火墙和selinux

一、Samba简介

Samba是通过CIFS协议实现windows和linux之间的文件共享的软件,本文要实现:

  1. 所有人都能访问公共目录;

  2. 每个人能访问自己的家目录;

  3. 其他人不能访问自己的目录。

二、samba相关包

1、samba程序包

# yum install samba
==============================================================================================
 Package                     Arch            Version                   Repository        Size
==============================================================================================
Installing:
 samba                       x86_64          4.4.4-14.el7_3            updates          610 k
Installing for dependencies:
 libldb                      x86_64          1.1.26-1.el7              base             125 k
 libtalloc                   x86_64          2.1.6-1.el7               base              34 k
 libtdb                      x86_64          1.3.8-1.el7_2             base              45 k
 libtevent                   x86_64          0.9.28-1.el7              base              34 k
 libwbclient                 x86_64          4.4.4-14.el7_3            updates          100 k
 pytalloc                    x86_64          2.1.6-1.el7               base              15 k
 samba-client-libs           x86_64          4.4.4-14.el7_3            updates          4.6 M
 samba-common                noarch          4.4.4-14.el7_3            updates          191 k
 samba-common-libs           x86_64          4.4.4-14.el7_3            updates          161 k
 samba-common-tools          x86_64          4.4.4-14.el7_3            updates          451 k
 samba-libs                  x86_64          4.4.4-14.el7_3            updates          260 k

==============================================================================================

2、samba相关文件与程序

#samba服务的主配置文件
/etc/samba/smb.conf

#NetBIOS Name与IP的对应关系,做用类似于/etc/hosts,一般无需配置
/etc/samba/lmhosts

#smbd, nmbd服务自定义参数配置文件
/etc/sysconfig/samba

# samba用户和密码数据库文件
/var/lib/samba/private/{passdb.tdb,secrets.tdb}

#Samba 3.0以后,默认使用tdb库(Trivial DataBase),这两个命令用于查看和操作tdb库,不过查看出的结果有点类似于16进制的数据,一般不使用,需安装 tdb-tools 包
/usr/bin/{tdbdump,tdbtool}

#smbpasswd命令用于创建samba用户和修改密码,类型于passwd命令的功能。Samba 3.0后使用pdb库,pdbedit命令为新版本中的samba用户信息管理命令
/usr/bin/{smbpasswd,pdbedit}

三、samba服务配置

/etc/samba/smb.conf

[global]
    #工作组
    workgroup = WORKGROUP 
    #欢迎信息
    server string = Samba Server Version %v 
    #netbios
    netbios name = ShareSERVER 
    #日志文件位置 
    log file = /var/log/samba/log.%m
    # maximum size of 50KB per log file, then rotate:
    max log size = 50
    #linux下使用的编码
    unix charset = utf8
    #验证方式,默认是user
    security = user
    #开启匿名访问
    map to guest = Bad User
    #指定用户名密码存储的数据格式
    passdb backend = tdbsam
    #关闭打印机
    disable spoolss = yes
    load printers = no
    cups options = raw
[public]
    #提示信息
    comment = public share
    #共享目录
    path = /home/ftp/public
    #开启公共访问
        public = yes
    #开启写入权限
    writable = yes
    #让所有人目录可见
    browseable = yes
[xingzheng] 
    comment = xingzheng
    path = /home/ftp/xingzheng
    #writable = yes
    browseable = yes
    #允许访问的使用者
    valid users = xingzheng
    #允许写入的使用者
    write list = xingzheng
    printable = no
    #默认权限设置
    create mask = 0644
    directory mask = 0755
[caiwu]
    comment = caiwu
    path = /home/ftp/caiwu
    #writable = yes
    browseable = yes
    #允许访问的使用者
    valid users = caiwu
    #允许写入的使用者
    write list = caiwu
    printable = no
    #默认权限设置
    create mask = 0644
    directory mask = 0755

四、添加用户和组

###创建用户xingzheng和家目录,同时默认创建了用户组xingzheng
# useradd -d /home/ftp/xingzheng  -m -s /sbin/nologin  xingzheng
# useradd -d /home/ftp/caiwu  -m -s /sbin/nologin caiwu
###创建smb用户,smb用户必须是系统已经存在的用户,执行命令后输入两次密码即创建成功
# pdbedit  -a -u xingzheng
# pdbedit  -a -u caiwu
###查看创建的用户
# pdbedit -L
xingzheng:1001:
caiwu:1002:

五、修改目录权限

使用useradd创建用户xingzheng和caiwu时,其家目录以配好相应的权限了

##公共目录
# chown nobody:nobody public
# ls -l ftp/
drwx------ 2 caiwu     caiwu     62 Aug  1 19:26 caiwu
drwxr-xr-x 2 nobody    nobody     6 Aug  1 18:40 public
drwx------ 2 xingzheng xingzheng 62 Aug  2 10:18 xingzheng

六、测试

用Windows资源管理器打开共享地址,可以看到三个共享目录,打开caiwu或xingzheng目录需要输入账户密码,无需账户密码就可以访问public

未分类

以caiwu账户可以登录使用caiwu目录,登录xingzheng目录则被拒绝

未分类

samba的匿名共享和用户认证共享配置

一、匿名共享

1、安装

yum -y  install samba*  ##安装samba软件

2、修改配置文件

vi  /etc/samba/smb.conf 
[global]

        workgroup = MYGROUP

        server string = Samba Server Version %v

        log file = /var/log/samba/log.%m

        max log size = 50

        security = share

        passdb backend = tdbsam

        load printers = yes

        cups options = raw

[public]

        comment = Public Stuff

        path = /opt/share   #共享目录

        public = yes

        writable = yes

        printable = no

:wq


mkdir  /opt/share

touch /opt/share/a.file

chmod 777 /opt/share

testparm   ##测试配置是否有误

/etc/init.d/smb  start

chkconfig smb on

netstat -utpln |grep mb  ##TCP:139,445/UDP:137,138查看端口

3、测试:

windows:

\10.10.10.95public

linux:

mount //10.10.10.95/public  /mnt

用户认证的共享:注意匿名和认证选一个

二、用户认证共享

1、创建用户

useradd user01

useradd user02

pdbedit -a user01  ##将系统用户添加为smb用户

pdbedit -a user02

pdbedit -L  ##查看smb用户

2、修改配置文件

[root@smb ~]# vi /etc/samba/smb.conf
[global]


        workgroup = MYGROUP

        server string = Samba Server Version %v

        log file = /var/log/samba/log.%m

        max log size = 50

        security = user    ##用户认证的共享

        passdb backend = tdbsam

        load printers = yes

        cups options = raw

[public]

        comment = Public Stuff

        path = /opt/share

        public = no   ##共享非公开

        writable = no   ##默认不可以写

        valid users = user01, user02  ##允许访问的用户

        write list = user01  ##有写入权限的用户

:wq

testparm

/etc/init.d/smb  restart

netstat -utpln |grep mb

3、测试

linux:客户机

mount  -o  username=u01  //10.10.10.95/public  /mnt

cd /mnt

touch b.file

ls

windows:

\10.10.10.95public

saltstack根据不同主机下发不同配置

[root@yy01 salt]# cat top.sls
base:
'*':
- conf
[root@yy01 salt]# cat conf.sls
{% for x in grains['nginx_ip'] %}
/opt/test01:
file.managed:
- source: salt://{{x}}/user.txt
- mode: 644
- user: root
- group: root
{% endfor %}

source内容需要存放在sls文件的目录下,本来想sls文件和数据分开来存放的。找了好多资料没有成功后续再研究吧

# salt * state.highstate

sls文件中的nginx_ip是自己定义的这个需要在配置salt客户端时就指定好的

CentOS6.8使用rsync sersync实现数据实时同步

Sersync简介

Sersync利用inotify与rsync对服务器进行实时同步,其中inotify用于监控文件系统事件,rsync是目前广泛使用的同步算法,其优点是只对文件不同的部分进行操作,所以其优势大大超过使用挂接文件系统的方式进行镜像同步。由金山的周洋开发完成,是目前使用较多的文件同步工具之一。该工具和其他的工具相比有如下优点:

  • sersync是使用c++编写,由于只同步发生更改的文件,因此比其他同步工具更节约时间、带宽;

  • 安装方便、配置简单;

  • 使用多线程进行同步,能够保证多个服务器实时保持同步状态;

  • 自带出错处理机制,通过失败队列对出错的文件重新出错,如果仍旧失败,则每10个小时对同步失败的文件重新同步;

  • 自带crontab功能,只需在xml配置文件中开启,即可按您的要求,隔一段时间整体同步一次;
    自带socket与http协议扩展,你可以方便的进行二次开发;

rsync+sersync与rsync+Inotify-tools区别

Inotify-tools只能记录下被监听的目录发生了变化(包括增加、删除、修改),并没有把具体是哪个文件或者哪个目录发生了变化记录下来;

rsync在同步的时候,并不知道具体是哪个文件或者哪个目录发生了变化,每次都是对整个目录进行同步,当数据量很大时,整个目录同步非常耗时(rsync要对整个目录遍历查找对比文件),因此,效率很低。

sersync是基于Inotify开发的,类似于Inotify-tools的工具;

sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或某一个目录的名字;

rsync在同步的时候,只同步发生变化的这个文件或者这个目录(每次发生变化的数据相对整个同步目录数据来说是很小的,rsync在遍历查找比对文件时,速度很快),因此,效率很高。

所以,当同步的目录数据量不大时,建议使用Rsync+Inotify-tools;当数据量很大(几百G甚至1T以上)、文件很多时,建议使用Rsync+sersync。

地址:http://code.google.com/p/sersync/,要翻墙才能下载

http://download.csdn.net/detail/hellopengyl/9918625

安装、配置

和Inotify-tools一样,只需要在数据源端安装

1、查看服务器内核是否支持inotify

[root@localhost src]# ll /proc/sys/fs/inotify    #查看服务器内核是否支持inotify,出现下面的内容,说明服务器内核支持inotify
total 0
-rw-r--r-- 1 root root 0 Jul 27 10:32 max_queued_events
-rw-r--r-- 1 root root 0 Jul 27 10:32 max_user_instances
-rw-r--r-- 1 root root 0 Jul 27 10:32 max_user_watches
[root@localhost src]# uname -r        #Linux下支持inotify的内核最小为2.6.13
2.6.32-642.el6.x86_64
[root@localhost src]# sysctl -a|egrep -i "max_queued_events|max_user_watches|max_user_instances"    #修改inotify默认参数(inotify默认内核参数值太小)
fs.inotify.max_user_instances = 128
fs.inotify.max_user_watches = 8192
fs.inotify.max_queued_events = 16384
fs.epoll.max_user_watches = 201420
[root@localhost src]# vim /etc/sysctl.conf
fs.inotify.max_user_instances = 65535
fs.inotify.max_user_watches = 99999999
fs.inotify.max_queued_events = 99999999    
[root@localhost src]# cat /proc/sys/fs/inotify/{max_user_instances,max_user_watches,max_queued_events}
65535
99999999
99999999
[root@localhost src]#
  • max_queued_events inotify队列最大长度,如果值太小,会出现”** Event Queue Overflow **”错误,导致监控文件不准确

  • max_user_watches 要同步的文件包含多少目录,可以用:find /home/www.osyunwei.com -type d | wc -l统计,必须保证max_user_watches值大于统计结果(这里/home/www.osyunwei.com为同步文件目录)

  • max_user_instances 每个用户创建inotify实例最大值

2、安装、配置sersync

[root@localhost src]# tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@localhost src]# mv GNU-Linux-x86 /app/sersync
[root@localhost src]# cd /app/sersync/
[root@localhost sersync]# ls
confxml.xml  sersync2
[root@localhost sersync]# cp confxml.xml{,.default}
[root@localhost sersync]# vim confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
    <host hostip="localhost" port="8008"></host>    #针对插件的保留字段,保留默认即可。
    <debug start="true"/>    #在sersync正在运行的控制台,打印inotify,rsync同步命令
    <fileSystem xfs="false"/>    #对于xfs文件系统用户,需要将这个选项开启才正常工作
    <filter start="false">    #过滤系统的临时文件,被过滤的文件不会被监控提高,默认过滤系统的临时文件(以“.”开头,以“~”结尾)
    <exclude expression="(.*).svn"></exclude>
    <exclude expression="(.*).gz"></exclude>
    <exclude expression="^info/*"></exclude>
    <exclude expression="^static/*"></exclude>
    </filter>
    <inotify>    #inotify监控文件模块
    <delete start="true"/>    #如果本地文件删除,不需要删除远程段的文件可以设置成false
    <createFolder start="true"/> #如果将createFolder设为false,则不会对产生的目录进行监控,该目录下的子文件与子目录也不会被监控;
    <createFile start="false"/>  #把createFile(监控文件事件选项)设置为false来提高性能,减少rsync通讯;因为拷贝文件到监控目录会产生create事件与close_write事件,所以如果关闭create事件,只监控文件拷贝结束时的时间close_write,同样可以实现文件完整同步;
    <closeWrite start="true"/>
    <moveFrom start="true"/>
    <moveTo start="true"/>
    <attrib start="false"/>
    <modify start="false"/>
    </inotify>

    <sersync>    #进行数据同步的模块
    <localpath watch="/app/rsync_client">    #定义本地要同步的目录
        <remote ip="10.15.43.100" name="app_rsync_server"/>#远程接受同步的IP和rsync模块名
        <!--<remote ip="192.168.8.39" name="tongbu"/>-->
        <!--<remote ip="192.168.8.40" name="tongbu"/>-->
    </localpath>
    <rsync>
        <commonParams params="-artuz"/>    #自定义rsync参数,默认是-artuz
        <auth start="true" users="rsync" passwordfile="/etc/rsyncd.secret"/>    #开启用户认证,定义用户名密码
        <userDefinedPort start="false" port="874"/><!-- port=874 -->
        <timeout start="false" time="100"/><!-- timeout=100 -->
        <ssh start="false"/>    #开启会使用rsync -e ssh的方式进行传输
    </rsync>
    <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->  #对于失败的传输,会进行重新传送,再次同步失败后日志记录到/tmp/rsync_fail_log.sh,并且每60分钟对失败的log进行重新同步
    <crontab start="true" schedule="600"><!--600mins-->    #每隔600s会做一次完全同步
        <crontabfilter start="false">    #如果开启了filter文件过滤功能,那么crontab整体同步也需要设置过滤,否则虽然实时同步的时候文件被过滤了,但crontab整体同步的时候 如果不单独设置crontabfilter,还会将需过滤的文件同步到远程,
        <exclude expression="*.php"></exclude> #crontab的过滤正则与filter过滤的不同,果同时开启了filter与crontab,则要开启crontab的crontabfilter,并按示例设置使其与filter的过滤一一对应。
        <exclude expression="info/*"></exclude>
        </crontabfilter>
    </crontab>
        <plugin start="false" name="command"/>    #当设置为true的时候,将文件同步到远程服务器后会调用name参数指定的插件。
    </sersync>

    <plugin name="command">    #name指定的插件
    #当文件同步完成后,会调用command插件,例如同步文件是file.txt,file.txt文件在改动之后,调用rsync同步到远程服务器后,调用command插件,执行/bin/sh file.txt suffix >/dev/null 2>&1
    #如果suffix 设置了,则会放在inotify事件file.txt之后,如果ignoreError为true,则会添加>/dev/null 2>&1
    <param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
    <filter start="false">
        <include expression="(.*).php"/>    #当filter为ture,include可以只对正则匹配到的文件,调用command。
        <include expression="(.*).sh"/>
    </filter>
    </plugin>
        #http插件,可以向指定域名的主机post,inotify监控的事件。
        #socket插件,开启该模块,则向指定ip与端口发送inotify所产生的文件路径信息
    <plugin name="socket">    
    <localpath watch="/opt/tongbu">
        <deshost ip="192.168.138.20" port="8009"/>
    </localpath>
    </plugin>
    #在同步过程中将文件发送到目的服务器后刷新cdn接口。如果不想使用,则设置<plugin start="false" name="refreshCDN"/>
    #该模块根据chinaCDN的协议,进行设计,当有文件产生的时候,就向cdn接口发送需要刷新的路径位置
    <plugin name="refreshCDN">    
    <localpath watch="/data0/htdocs/cms.xoyo.com/site/">  #需要监控的目录
            #cdnifo标签制定了cdn接口的域名,端口号,以及用户名与密码。
        <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
        #sendurl标签是需要刷新的url的前缀
        <sendurl base="
        #regexurl标签中的,regex属性为true时候,使用match属性的正则语句匹配inotify返回的路径信息,并将正则匹配到的部分作为url一部分,
        <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
            #如果产生文件事件为:/data0/htdoc/cms.xoyo.com/site/jx3.xoyo.com/image/a/123.txt
            #经过上面的match正则匹配后,最后刷新的路径是:http://pic.xoyo.com/cms/jx3/a/123.txt;
            #如果regex属性为false,最后刷新的路径是http://pic.xoyo.com/cms/jx3.xoyo.com/images/a/123.txt;
    </localpath>
    </plugin>
</head>
[root@localhost sersync]# /app/sersync/sersync2 -d -r -n 8 -o /app/sersync/confxml.xml
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -d    run as a daemon
option: -r    rsync all the local files to the remote servers before the sersync work
option: -n    thread num is:  8
option: -o    config xml name:  /app/sersync/confxml.xml
parse xml config file
host ip : localhost  host port: 8008
Open debug, you will see debug infomation 
daemon start,sersync run behind the console 
Start the crontab    Every 600 minutes rsync all the files to the remote servers entirely
use rsync password-file :
user is    rsync
passwordfile is   /etc/rsyncd.secret
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 10  = 1(primary thread) + 1(fail retry thread) + 8(daemon sub threads) 
Max threads numbers is: 18 = 10(Thread pool nums) + 8(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /app/rsync_client && rsync -artuz -R --delete ./ [email protected]::app_rsync_server --password-file=/etc/rsyncd.secret
crontab command:cd /app/rsync_client && rsync -artuz -R --delete ./ [email protected]::app_rsync_server --password-file=/etc/rsyncd.secret
run the sersync: 
watch path is: /app/rsync_client
add watch: /app/rsync_client return wd is: 1
add watch: /app/rsync_client/test return wd is: 2
[root@localhost sersync]#
  • -d 后台启动

  • -r 同步前将已存在的文件全部同步过去,如果设置了过滤器,即在xml文件中,filter为true,则暂时不能使用-r参数进行整体同步;

  • -n 开启的线程总数默认10

  • -o 指定配置文件,指定 -o 参数可以指定多个不同的配置文件,从而实现sersync多进程多实例的数据同步

  • -m 不进行同步,只运行插件 ./sersync -m pluginName 例如:./sersync -m command,则在监控到事件后,不对远程目标服务器进行同步,而是直接运行command插件

[root@localhost sersync]# cat monitor_sersync.sh #监控Sersync运行状态的脚本,如果服务停止了就重启服务
#!/bin/bash
server_file="/app/sersync/sersync2"
conf_file="/app/sersync/confxml.xml"
options="-d -r -n 8 -o"
proc_num=$(ps -ef|grep -i sersync2|grep -v "grep"|wc -l)
if [ $proc_num -lt 1 ];then
    cd $(dirname $server_file)
    nohup $server_file $options $conf_file &
else
   exit 0;
fi
[root@localhost sersync]# chmod +x monitor_sersync.sh 
[root@localhost sersync]# crontab -l
*/5 * * * *  /app/sersync/monitor_sersync.sh
[root@localhost sersync]# /etc/init.d/crond restart
[root@localhost sersync]# vim /etc/rc.d/rc.local    #设置开机自动运行脚本
/app/sersync/sersync2 -d -r -n 8 -o /app/sersync/confxml.xml
[root@localhost sersync]#

测试

在客户端监控的目录/app/rsync_client创建文件,然后查看服务器端app_rsync_server模块对应的目录是否同步更新

[root@localhost rsync_client]# touch file{1..9}
inotify wd:1   name:file1  mask:256
inotify wd:1   name:file1  mask:8
inotify wd:1   name:file2  mask:256
inotify wd:1   name:file2  mask:8
inotify wd:1   name:file3  mask:256
inotify wd:1   name:file3  mask:8
inotify wd:1   name:file4  mask:256
inotify wd:1   name:file4  mask:8
inotify wd:1   name:file5  mask:256
[root@localhost rsync_client]# cd /app/rsync_client && rsync -artuz -R "./file2" [email protected]::app_rsync_server --password-file=/etc/rsyncd.secret
inotify wd:1   name:file5  mask:8
inotify wd:1   name:file6  mask:256
inotify wd:1   name:file6  mask:8
inotify wd:1   name:file7  mask:256
inotify wd:1   name:file7  mask:8
inotify wd:1   name:file8  mask:256
inotify wd:1   name:file8  mask:8
inotify wd:1   name:file9  mask:256
inotify wd:1   name:file9  mask:8
cd /app/rsync_client && rsync -artuz -R "./file1" [email protected]::app_rsync_server --password-file=/etc/rsyncd.secret
cd /app/rsync_client && rsync -artuz -R "./file3" [email protected]::app_rsync_server --password-file=/etc/rsyncd.secret
cd /app/rsync_client && rsync -artuz -R "./file4" [email protected]::app_rsync_server --password-file=/etc/rsyncd.secret
cd /app/rsync_client && rsync -artuz -R "./file5" [email protected]::app_rsync_server --password-file=/etc/rsyncd.secret
cd /app/rsync_client && rsync -artuz -R "./file6" [email protected]::app_rsync_server --password-file=/etc/rsyncd.secret
cd /app/rsync_client && rsync -artuz -R "./file7" [email protected]::app_rsync_server --password-file=/etc/rsyncd.secret
cd /app/rsync_client && rsync -artuz -R "./file8" [email protected]::app_rsync_server --password-file=/etc/rsyncd.secret
cd /app/rsync_client && rsync -artuz -R "./file9" [email protected]::app_rsync_server --password-file=/etc/rsyncd.secret
[root@localhost rsync_client]# touch file1
inotify wd:1   name:file1  mask:8
[root@localhost rsync_client]# cd /app/rsync_client && rsync -artuz -R "./file1" [email protected]::app_rsync_server --password-file=/etc/rsyncd.secret

[root@localhost sersync]#

压测

写入10K个文件批量同步测试结果:

[root@localhost rsync_client]# for n in {1..10000};do dd if=/dev/zero of=/app/rsync_client/"$n".txt bs=1M count=5;done

查看同步速度,当10K个文件同步完后,在/app/rsync_server里发现才同步了600多个文件

多实例情况

配置多个confxml.xml文件(比如:www、bbs、blog….等等),根据不同的需求同步对应的实例文件

[root@localhost rsync_client]# /app/sersync/sersync2 -d -r -n 8 -o /app/sersync/www_confxml.xml
[root@localhost rsync_client]# /app/sersync/sersync2 -d -r -n 8 -o /app/sersync/bbs_confxml.xml
[root@localhost rsync_client]# /app/sersync/sersync2 -d -r -n 8 -o /app/sersync/blog_confxml.xml

rsync本地复制、比对文件、增量同步示例

A fast,versatile,remote (and local) file-copying tool.

rsync基于ssh协议实现高效率远程或本地文件复制,传输速度比scp快。复制文件时会比对本地文件与远程主机的文件,仅复制有差异的文件。

常用选项:

-q,--quiet:suppress non-error messages 静默模式
-v,--verbose:increase verbosity
-a,--archive:archive mode; equals -rlptgoD (no -H,-A,-X) 归档模式,相当于-rlptgoD,不包括(no -H,-A,-X);最常用的参数
-H,--hard-links:preserve hard links 保留硬链接
-A,--acls:preserve ACLs (implies --perms) 保留ACL权限
-X,--xattrs:preserve extended attributes 保留扩展属性
-c, --checksum:skip based on checksum, not mod-time & size
-r,--recursive:recurse into directories 递归
-l,--links:copy symlinks as symlinks 保留软链接,而不跟踪原文件
-p,--perms:preserve permissions 保留权限
-t,--times:preserve modification times 保留mtime
-g,--group:preserve group 保留属组
-o,--owner:preserve owner (super-user only) 保留属主
-D:same as --devices,--specials 保留设备文件和特殊文件
--devices:preserve device files (super-user only)
--specials:preserve special files

-z,--compress:compress file data during the transfer 输过程中压缩文件数据
-n, --dry-run:perform a trial run with no changes made 干跑测试
-u,--update:skip files that are newer on the receiver 增量同步,跳过比本地较新的文件
--delete:delete extraneous files from destination dirs 删除目标目录多余文件
--progress:show progress during transfer 显示传输进度

本地复制

# rsync -av ansible_auto/public/uy-s-192-v01.cfg objects/ansible_auto/public/uy-s-192-v01.cfg

比对文件

# cd /usr/local/nagios/etc/
# rsync -acvn ansible_auto/ objects/ansible_auto/ > diff

增量同步

# rsync -avzu --progress /opt/* [email protected]:/opt/

另外,可以设置计划任务实时同步和备份文件

# crontab -e
*/1 * * * * rsync -avzu /opt/media/* 192.168.201.123:/opt/media

python的dict和json有什么区别?

工作中和其他语言的工程师交流,合作与联调中经常会涉及到数据的传输,这个数据的传输通常为json字符串,这个json格式数据和python自身的dict数据对象非常像,所以很自然的会思考这两者究竟区别在哪里?

首先,两者不一样

区别

  • Python 的字典是一种数据结构,JSON 是一种数据格式。

json 就是一个根据某种约定格式编写的纯字符串,不具备任何数据结构的特征。而 python 的字典的字符串表现形式的规则看上去和 json 类似,但是字典本身是一个完整的数据结构,实现了一切自身该有的算法。

  • Python的字典key可以是任意可hash对象,json只能是字符串。

形式上有些相像,但JSON是纯文本的,无法直接操作。

  1. python dict 字符串用单引号,json强制规定双引号。
  2. python dict 里可以嵌套tuple,json里只有arrayjson.dumps({1:2}) 的结果是 {”1″:2}json.dumps((1,2)) 的结果是[1,2]
  3. json key name 必须是字符串, python 是hashable, {(1,2):1} 在python里是合法的,因为tuple是hashable type;{[1,2]:1} 在python里TypeError: unhashable “list”
  4. json: true false null python:,True False None
  5. python {“me”: “我”} 是合法的 json 必须是 {“me”: “u6211”}

联系

  • dict 存在于内存中,可以被序列化成 json 格式的数据(string),之后这些数据就可以传输或者存储了。

  • JSON 是一种数据传输格式。

  • 也就是说,这些字符串以 JSON 这样的格式来传输,至于你怎么 parse 这些信息,甚至是是否 parse,是否储存,都不是 JSON 的事情。

  • 用 Python 举个例子: 某段程序可以把字符串 “{A:1, B:2}” parse 成 一对 tuple( (“A”, 1), (“B”, 2) )而不是 dictionary {“A”: 1, “B”: 2}。Python 的 dictionary 是对 Hash Table 这一数据结构的一种实现。它使用其内置的哈希函数来规划键对应的内容的储存位置,从而获得 O(1) 的数据读取速度。所以 JSON 是一种数据传输格式,它能被解析成 Python 的 Dictionary 或者其他形式,但解析成什么内容是和 JSON 这种格式无关的。Python 的 Dictionary 则是 Python 对 Hash Table 的实现,一套从存储到提取都封装好了的方案。

CentOS 7系统Nginx申请免费SSL安全证书Let’s Encrypt教程

Let’s Encrypt是国外一个公共的免费SSL项目,由 Linux 基金会托管,它的来头不小,由Mozilla、思科、Akamai、IdenTrust和EFF等组织发起,目的就是向网站自动签发和管理免费证书,以便加速互联网由HTTP过渡到HTTPS,目前Facebook等大公司开始加入赞助行列。

Let’s Encrypt虽然还在测试当中,但是市场需求非常大,已经有非常多的朋友迫不及待地安装并用上了Let’s Encrypt。Let’s Encrypt向广大的网站提供免费SSL证书,不管是对于网站站长、互联网用户,还是对整个Web互联网,都是非常有利的,它有利于整个互联网的安全。

本篇文章就来为大家讲解一下如何获取Let’s Encrypt免费SSL证书,并附上Nginx的SSL证书配置方法。

未分类

使用 HTTPS 前的一些疑惑

现在是 2017 年,使用 HTTPS 已经不像几年前是一件昂贵的事情。当然我也是自己了解了一圈才消除了自己的疑惑,主要是:

  • 我的网站(一个简单的博客)可能没必要使用 HTTPS
  • HTTPS 会不会让网站速度变慢?
  • HTTPS 需要花钱吧?证书好像不便宜
  • 配置和维护 HTTPS 代价很高

要回答这些问题,推荐大家去看一下 Google I/O 2016 的视频(Youtube):https://www.youtube.com/watch?v=YMfW1bfyGSY

视频里把所有问题都详细一一解答,强烈推家把视频看完。

我简单总结:

  • 每个网站都应该用 HTTPS,就算是全静态站点也同样如此,运营商劫持严重干扰访问者的体验
  • 有几项技术可以提高 HTTPS 的性能,包括 Strict Transport Security,TLS False Start 和 HTTP/2 ,这些技术让 HTTPS 速度不慢,某些情况下会甚至更快
  • HTTPS 针对个人单个(或者几个)域名的使用来说,已经是免费的
  • 配置和维护 HTTPS 异常简单,Let’s Encrypt 这个项目通过自动化把事情简单化了

有哪些靠谱的免费 HTTPS 证书提供商?

选择证书提供商有3个主要考量:

  • 浏览器和操作系统支持程度
  • 证书类型
  • 维护成本

1、浏览器和操作系统支持程度

基本你能查到的热门证书提供商,支持程度都不会太差。例如 Let’s Encrypt 的支持可以访问:Which browsers and operating systems support Let’s Encrypt

可以看到,Android 2.3.6 以上,Firefox 2.0 以上,Windows Vista 以上,iOS 3.1 以上,Google Chrome全平台都是支持的。这一点就不用太担心了,看你你的网站受众情况来决定。对于我来说,我完全不在乎 Windows XP 的 IE 用户。

2、证书类型

HTTPS 证书分为3类, 1. DV 域名验证证书 2. OV 组织机构验证证书 3. EV 增强的组织机构验证证书。每类证书在审核和验证方面要求严格程度不同,浏览器会在地址栏给予不同证书不一样的展现。

一般个人使用DV证书完全够了,浏览器表现为地址栏前会有绿色的小锁。下面聊到的免费证书都是 DV 域名验证证书。

3、维护成本

我调研不多,使用过 StartSSL,现在用 Let’s Encrypt。StartSSL 的免费证书有效期是1年,1年后需要手动更换。配置过程还挺麻烦的。

更推荐 Let’s Encrypt,虽然有效期只有3个月,但可以用 certbot 自动续期,完全不受影响。而且 Let’s Encrypt 因为有了 certbot 这样的自动化工具,配置管理起来非常容易。

4、如何安装

第一种办法:通过官网申请安装:

  • Let’s Encrypt官网:

官方网站:https://letsencrypt.org/
项目主页:https://github.com/letsencrypt/letsencrypt

  • 生成 Let’s Encrypt 证书

未分类

Let’s Encrypt 证书生成不需要手动进行,官方推荐 certbot 这套自动化工具来实现。3步轻松搞定:

  • 下载安装 certbot (Let’s Encrypt项目的自动化工具)
  • 创建配置文件
  • 执行证书自动化生成命令

你可以在 certbot 网站上,选择你的 Web Server 和 操作系统,就能看到对应的安装和配置教程。

(本文主要介绍下面的安装办法)

第二种办法:手动申请安装(由于 CentOS的复杂性,我使用的这种办法)

一、 安装Let’s Encrypt免费SSL准备

安装Let’s Encrypt脚本依赖环境:(这一部分可以跳过,因为官方提供的Let’s Encrypt脚本会自动检测并安装)

# Debian

apt-get install git

# CentOS 7

yum install epel-release

cd /root/

wget https://dl.eff.org/certbot-auto --no-check-certificate

chmod +x

./certbot-auto

./certbot-auto -n

./certbot-auto -n只是用来安装依赖包的,也可以跳过直接到下面的生成证书的步骤,国内VPS或服务器上使用的话建议先修改为国内的pip源。

二、Let’s Encrypt免费证书生成

单域名生成证书:

./certbot-auto certonly --email [email protected] --agree-tos --no-eff-email --webroot -w /home/wwwroot/www.smusic.cc -d www.smusic.cc

多域名单目录生成单证书:(即一个网站多个域名使用同一个证书)

./certbot-auto certonly --email [email protected] --agree-tos --no-eff-email --webroot -w /home/wwwroot/www.smusic.cc -d www.vpser.net -d bbs.smusic.cc

多域名多目录生成一个证书:(即一次生成多个域名的一个证书)

./certbot-auto certonly --email [email protected] --agree-tos --no-eff-email --webroot -w /home/wwwroot/www.smusic.cc -d www.smusic.cc -d bbs.smusic.cc -w /home/wwwroot/smusic.cc -d www.smusic.cc -d smusic.cc

提示:

IMPORTANT NOTES:

– Congratulations! Your certificate and chain have been saved at
/etc/letsencrypt/live/www.vpser.net/fullchain.pem. Your cert will
expire on 2016-10-01. To obtain a new or tweaked version of this
certificate in the future, simply run certbot-auto again. To
non-interactively renew *all* of your certificates, run
“certbot-auto renew”
– If you like Certbot, please consider supporting our work by:
Donating to ISRG / Let’s Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le

就是生成 成功。

生成的证书会存在:/etc/letsencrypt/live/www.smusic.cc/ 目录下

三、证书续期

cerrbot的续期比原来的更加简单,因为证书只有90天,所以建议使用crontab进行自动续期:

crontab 里加上如下规则:0 3 */5 * * /root/certbot-auto renew –renew-hook “/etc/init.d/nginx reload” 这样每5天就会执行一次所有域名的续期操作。当然时间也可以自行进行调整,建议别太频繁,因为他们都有请求次数的限制,如果需要强制更新可以在前面命令上加上 –force-renew 参数。

具体方法参考:www.smusic.cc/2094.html

四、SSL证书注意事项:

1、因为默认LNMP的虚拟主机里是禁止 . 开头的隐藏文件及目录的,所以访问http://abc.com/.well-known/acme-challenge/**** 这个链接的话返回403错误,所以必须要将对应虚拟主机配置文件里的

location ~ /.
{
deny all;
}
这段配置删掉或注释掉或在这段配置前面加上
location ~ /.well-known {
allow all;
}

以上配置代码,然后重启nginx。

2、如果要启用http2的话,建议编辑lnmp.conf,将里面的Nginx_Modules_Options的单引号里加上 –with-openssl=/root/openssl-1.0.2h

并执行:

cd /root && wget -c https://www.openssl.org/source/openssl-1.0.2h.tar.gz && tar zxf openssl-1.0.2h.tar.gz 

然后使用升级脚本 ./upgrade.sh nginx 升级nginx至1.9.5或更高版本。

3、国内有些用户反映会卡在Installing Python packages…这个地方不动,因为pip的默认源是国外的,国内可能会有点慢,可以执行下面命令来修改pip源为国内的:

mkdir ~/.pip
cat > ~/.pip/pip.conf <<EOF
[global]
index-url = https://pypi.doubanio.com/simple/

[install]
trusted-host=pypi.doubanio.com
EOF

执行完,再重新运行certbot的命令应该正常安装python的包了。

五、Nginx虚拟主机的设置

配置文件地址: /usr/local/nginx/conf/vhost/www.smusic.cc.conf (根据自己的环境确认)

接下来进行配置Nginx虚拟主机文件,完整配置如下:

server
{
listen 443 ssl;   #lnmp1.3+版本默认支持http2,可以加上http2,lnmp1.2版本默认支持spdy,可以加上spdy
server_name www.smusic.cc;     ##这里是你的域名
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/www.smusic.cc;            #网站目录
ssl_certificate /etc/letsencrypt/live/www.smusic.cc/fullchain.pem;    #前面生成的证书,改一下里面的域名就行
ssl_certificate_key /etc/letsencrypt/live/www.smusic.cc/privkey.pem;   #前面生成的密钥,改一下里面的域名就行
ssl_ciphers "EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5";
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;

include wordpress.conf;  #这个是伪静态根据自己的需求改成其他或删除
#error_page 404 /404.html;
location ~ [^/].php(/|$)
{
# comment try_files $uri =404; to enable pathinfo
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;     #lnmp 1.0及之前版本替换为include fcgi.conf;
#include pathinfo.conf;
}

location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}

location ~ .*.(js|css)?$
{
expires 12h;
}

access_log off;
}

需将上述配置根据自己的实际情况修改后,添加到虚拟主机配置文件最后面。

添加完需要执行:/etc/init.d/nginx reload 重新载入配置使其生效。
重启Nginx:/etc/init.d/nginx restart

如果需要HSTS,可以加上add_header Strict-Transport-Security “max-age=63072000; includeSubdomains; preload”; 前面参数里的数值是秒。 开启后有效期内必须只能https访问,不确定的话不要开启。

LNMPA模式下只需要在Nginx上设置,只需要将php解析部分替换为 include proxy-pass-php.conf; 即可。

六、下面说一下可能会遇到的问题:

1、国内DNS服务商可能会不行,目前已知dnspod、cloudxns不行dnspod、cloudxns目前已经可以

Namecheap、Route 53的都可以。

2、Linode福利或IPv6用户福利

可能目前Linode用户应该遇到了

An unexpected error occurred:
There were too many requests of a given type :: Error creating new registration :: Too many registrations from this IP
Please see the logfiles in /var/log/letsencrypt for more details.

这个不一定是因为IP注册的次数过多,可能是因为IPv6的事,具体解决方法如下:

执行:sysctl -w net.ipv6.conf.all.disable_ipv6=1 来临时禁用IPv6
再生成证书后执行:sysctl -w net.ipv6.conf.all.disable_ipv6=0 再来解除禁用IPv6

3、如果启用了防火墙需要将443端口加入允许规则,一般iptables可以参考:https://www.vpser.net/security/linux-iptables.html

4、如果LNMP下访问http://abc.com/.well-known/acme-challenge/**** 这个链接403的话是因为默认LNMP 1.3的虚拟主机里是禁止.开头的隐藏文件及目录的。

需要将

location ~ /.
{
deny all;
}
这段配置删掉或注释掉或在这段配置前面加上

location ~ /.well-known {
allow all;
}

以上配置代码,然后重启nginx。

5、同时这里提醒一下如果设置了http 301跳到https的用户,再续期前还需要在nginx设置如下:

让启用 Nginx 启用 SSL,两种方法,单一支持HTTPS,或者同时支持 HTTP 和 HTTPS 两种协议。

备注 Nginx 默认没有安装 SSL 模块,安装时需要手动添加 –with-http_ssl_module 参数。(如果您使用LNMP安装包,可以忽视,默认已安装了)
80端口的虚拟主机上需要添加上,不加的话会无法验证的

location /.well-known/ {
add_header Content-Type 'text/plain;';
root /网站目录完整路径;
}

附完整的nginx下301 http跳到https的配置:

server
{
listen 80;
server_name www.vpser.net;
location /.well-known/ {
add_header Content-Type 'text/plain;';
root /网站目录完整路径;
}
location / {
return 301 https://www.vpser.net$request_uri;
}
}

用专业在线工具测试你的服务器 SSL 安全性

Qualys SSL Labs 提供了全面的 SSL 安全性测试,填写你的网站域名,给自己的 HTTPS 配置打个分。

如果你完全按照我上面教程配置,遵循了最佳实践,你应该和我一样得分是 A+

未分类

这意味着你启用了HTTPS,现在足够的安全,并且使用了最新技术,保证了性能。

配置Zabbix监控MySQL详细教程

一、Server端安装

机器 192.168.94.78

1、创建mysql实例

机器:192.168.94.78
目录:/data/mysql6001

2、建立系统用户

[[email protected] zabbix_agent]# groupadd  -g 201 zabbix
[[email protected] zabbix_agent]# useradd -g zabbix -u 201 -m zabbix
[[email protected] zabbix_agent]# passwd zabbix

3、创建zabbix数据库

[[email protected] zabbix]# mysql6001
mysql> create database zabbix; 
Query OK, 1 row affected (0.00 sec)

mysql>  grant all on zabbix.* to zabbix@'%' identified by 'zabbix'; 
Query OK, 0 rows affected (0.00 sec)

mysql>  flush privileges;  
Query OK, 0 rows affected (0.00 sec)

4、上传zabbix包到server端机器

zabbix-2.4.4.tar.gz
解压
tar zxvf zabbix-2.4.4.tar.gz 
目录:zabbix-2.4.4

5、导入SQL

cd /data/zabbix/zabbix-2.4.4/database/mysql/
注:有顺序的
[[email protected] mysql]# mysql6001 zabbix < schema.sql 
[[email protected] mysql]# mysql6001 zabbix < images.sql 
[[email protected] mysql]# mysql6001 zabbix < data.sql 

6、导入网页

安装软件包

[[email protected] mysql]# yum -y  install curl curl-devel net-snmp net-snmp-devel perl-DBI php-gd php-xml php-bcmath
[[email protected] mysql]#yum -y install php 
[[email protected] mysql]#yum -y install httpd

[[email protected] html]# mkdir  /var/www/html/zabbix
[[email protected] html]# cp -a /data/zabbix/zabbix-2.4.4/frontends/php/*     /var/www/html/zabbix/
[[email protected] html]# chown -R  apache.apache /var/www/html/zabbix/

7、安装zabbix服务端

[[email protected] zabbix-2.4.4]# cd /data/zabbix/zabbix-2.4.4
[[email protected] zabbix-2.4.4]#  ./configure --prefix=/usr/local/zabbix --enable-server --enable-agent --enable-proxy --with-mysql=/usr/local/mysql5.5/bin/mysql_config --with-net-snmp --with-libcurl

第二步:make
第三步:make install

修改 vi /etc/services,添加下面几行

zabbix-agent    10050/tcp               #zabbix agent
zabbix-agent    10050/udp               #zabbix agent
zabbix-trapper  10051/tcp               #zabbix trapper
zabbix-trapper  10051/udp               #zabbix trapper

8、修改zabbix的配置:

[[email protected] zabbix-2.4.4]# vi /usr/local/zabbix/etc/zabbix_server.conf
[[email protected] zabbix-2.4.4]# cat /usr/local/zabbix/etc/zabbix_server.conf|grep -v ^# | grep -v ^$
LogFile=/data/zabbix/zabbix_server.log
PidFile=/data/zabbix/zabbix_server.pid
DBHost=192.168.94.78
DBName=zabbix
DBUser=zabbix
DBPassword=zabbix
DBSocket=/tmp/mysql.sock
DBPort=6001
StartPollers=20
StartTrappers=15
 StartPingers=10
User=zabbix

[root@localhost ~]# vi /etc/init.d/zabbix_server

BASEDIR=/usr/local/zabbix   ##########修改这行

[root@localhost ~]# vi /etc/init.d/zabbix_agentd

BASEDIR=/usr/local/zabbix   ##########修改这行

9、修改httpd配置

[[email protected] zabbix-2.4.4]# vi /etc/httpd/conf/httpd.conf 
ServerName 127.0.0.1
<VirtualHost *:80>
 DocumentRoot  "/var/www/html"
 ServerName 192.168.94.78
</VirtualHost>

10、开启httpd

[[email protected] core]# service httpd restart
Stopping httpd: [  OK  ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
[  OK  ]

[[email protected] core]# lsof -i:80
COMMAND   PID   USER   FD   TYPE     DEVICE SIZE/OFF NODE NAME
httpd   23923   root    4u  IPv4 2582967064      0t0  TCP *:80(LISTEN)
httpd   23925 apache    4u  IPv4 2582967064      0t0  TCP *:80 (LISTEN)
httpd   23926 apache    4u  IPv4 2582967064      0t0  TCP *:80 (LISTEN)
httpd   23927 apache    4u  IPv4 2582967064      0t0  TCP *:80 (LISTEN)
httpd   23928 apache    4u  IPv4 2582967064      0t0  TCP *:80 (LISTEN)
httpd   23929 apache    4u  IPv4 2582967064      0t0  TCP *:80 (LISTEN)
httpd   23930 apache    4u  IPv4 2582967064      0t0  TCP *:80 (LISTEN)
httpd   23931 apache    4u  IPv4 2582967064      0t0  TCP *:80 (LISTEN)
httpd   23932 apache    4u  IPv4 2582967064      0t0  TCP *:80 (LISTEN)

11、做几个软连接

[root@localhost ~]# ln -s /usr/local/zabbix/bin/* /usr/bin/
[root@localhost ~]# ln -s /usr/local/zabbix/sbin/* /usr/sbin/

[root@localhost ~]# cd  /data/zabbix/zabbix-2.4.4/misc/init.d/fedora/core

[root@localhost ~]# cp * /etc/init.d/

12、开启zabbix server端

[[email protected] subsys]# chown zabbix.zabbix   /var/lock/subsys/ -R
[[email protected] subsys]# chown zabbix.zabbix   /usr/local/zabbix  -R
[[email protected] subsys]# chown zabbix.zabbix  /data/zabbix/  -R

[[email protected] core]# /etc/init.d/zabbix_agentd start
Starting zabbix_agentd:  zabbix_agentd [24613]: user zabbix does not exist
zabbix_agentd [24613]: cannot run as root!
[FAILED]
[[email protected] core]# /etc/init.d/zabbix_server start
Starting zabbix_server:  zabbix_server [24821]: user zabbix does not exist
zabbix_server [24821]: cannot run as root!
[FAILED]

处理失败:
[[email protected] home]#  vi /etc/passwd 
改成:
zabbix:x:505:498::/home/zabbix:/bin/bash
[[email protected] home]# su - zabbix

===============================================
[zabbix_linux@bjs-dbmypacket3301 ~]$  /etc/init.d/zabbix_agentd restart
Shutting down zabbix_agentd: [  OK  ]
Starting zabbix_agentd:  [  OK  ]

[zabbix_linux@bjs-dbmypacket3301 ~]$  /etc/init.d/zabbix_server stop
Shutting down zabbix_server: [  OK  ]
[zabbix_linux@bjs-dbmypacket3301 ~]$  /etc/init.d/zabbix_server start
Starting zabbix_server:  [  OK  ]

13、页面设置zabbix

http://192.168.94.78/zabbix/setup.php

未分类

zabbix PHP mbstring.func_overload 报错

修复方法:

vi /etc/php.ini
mbstring.func_overload=off
lways_populate_raw_post_data=1
 重启httpd与zabbix
[[email protected] html]# service httpd restart
[zabbix@bjs-dbmypacket3301 ~]$ /etc/init.d/zabbix_server restart

未分类

未分类

未分类

未分类

未分类

未分类

未分类

二、客户端安装

客户端机器

192.168.94.144
192.168.94.137

1、安装agent

[[email protected] zabbix]# scp zabbix-2.4.4.tar.gz 192.168.94.144:/data/zabbix_agent/

[[email protected] zabbix_agent]# groupadd  -g 201 zabbix
[[email protected] zabbix_agent]# useradd -g zabbix -u 201 -m zabbix
[[email protected] zabbix_agent]# passwd zabbix
Changing password for user zabbix.
New password: 
BAD PASSWORD: it is based on a dictionary word
BAD PASSWORD: is too simple
Retype new password: 
passwd: all authentication tokens updated successfully.
 [[email protected] zabbix-2.4.4]# cd /data/zabbix_agent/zabbix-2.4.4
 [[email protected] zabbix-2.4.4]#  ./configure --prefix=/usr/local/zabbix  --enable-agent --enable-proxy --with-mysql=/usr/local/mysql5.5/bin/mysql_config 

[[email protected] zabbix-2.4.4]# make && make install 

2、拷贝/创建快捷

[[email protected] zabbix-2.4.4]# pwd
/data/zabbix_agent/zabbix-2.4.4
[[email protected] zabbix-2.4.4]# cp misc/init.d/tru64/zabbix_agentd   /etc/init.d/
[[email protected] zabbix-2.4.4]# chmod +x /etc/init.d/zabbix_agentd
3、修改配置文件
[[email protected] zabbix-2.4.4]# vi  /usr/local/zabbix/etc/zabbix_agentd.conf

[[email protected] zabbix-2.4.4]# cat /usr/local/zabbix/etc/zabbix_agentd.conf | grep -v ^# | grep -v ^$
LogFile=/data/zabbix_agent/zabbix_agentd.log
UnsafeUserParameters=1                   
Include= /usr/local/zabbix/etc/zabbix_agentd.conf.d/
Server=192.168.94.78      #server端IP
ServerActive=192.168.94.78   #server端IP
Hostname=192.168.94.144     #客户端IP

4、开启zabbix 的agent

[[email protected] zabbix-2.4.4]# chown zabbix.zabbix /data/zabbix_agent/ -R
[[email protected] zabbix-2.4.4]# chown zabbix.zabbix /usr/local/zabbix/  -R
[[email protected] zabbix-2.4.4]# chown zabbix.zabbix /soft/mysqlmonitor/port_6190 -R


[[email protected] tmp]# /usr/local/zabbix/sbin/zabbix_agentd -c /usr/local/zabbix/etc/zabbix_agentd.conf
[[email protected] tmp]# ps -ef|grep zabbix
zabbix   53114     1  0 16:14 ?        00:00:00 /usr/local/zabbix/sbin/zabbix_agentd -c /usr/local/zabbix/etc/zabbix_agentd.conf
zabbix   53115 53114  0 16:14 ?        00:00:00 /usr/local/zabbix/sbin/zabbix_agentd: collector [idle 1 sec]                    
zabbix   53116 53114  0 16:14 ?        00:00:00 /usr/local/zabbix/sbin/zabbix_agentd: listener #1 [waiting for connection]      
zabbix   53117 53114  0 16:14 ?        00:00:00 /usr/local/zabbix/sbin/zabbix_agentd: listener #2 [waiting for connection]      
zabbix   53118 53114  0 16:14 ?        00:00:00 /usr/local/zabbix/sbin/zabbix_agentd: listener #3 [waiting for connection]      
zabbix   53119 53114  0 16:14 ?        00:00:00 /usr/local/zabbix/sbin/zabbix_agentd: active checks #1 [idle 1 sec]             
root     53362 62129  0 16:14 pts/0    00:00:00 grep zabbix
[[email protected] tmp]# lsof -i:1005COMMAND     PID   USER   FD   TYPE     DEVICE SIZE/OFF NODE NAME
zabbix_ag 53114 zabbix    4u  IPv4 2301997410      0t0  TCP *:zabbix-agent (LISTEN)
zabbix_ag 53115 zabbix    4u  IPv4 2301997410      0t0  TCP *:zabbix-agent (LISTEN)
zabbix_ag 53116 zabbix    4u  IPv4 2301997410      0t0  TCP *:zabbix-agent (LISTEN)
zabbix_ag 53117 zabbix    4u  IPv4 2301997410      0t0  TCP *:zabbix-agent (LISTEN)
zabbix_ag 53118 zabbix    4u  IPv4 2301997410      0t0  TCP *:zabbix-agent (LISTEN)
zabbix_ag 53119 zabbix    4u  IPv4 2301997410      0t0  TCP *:zabbix-agent (LISTEN)

5、界面添加host

此时可以在界面上面添加host进行监控测试了,因后面添加自定义监控,此处忽略

三、自定义监控配置

1、准备检查脚本

准备检查mysql状态的脚本

[[email protected] ~]# cd /soft/port_6190/
[[email protected] port_6190]# ./mysql_status.pl|grep com_writes
com_writes=0

[[email protected] port_6190]# /soft/port_6190/mysql_status.pl |grep com_writes|awk -F '=' '{print $2}'
0

注:此处可以自定义脚本,返回key=value 即可,或者使用percona的模板,此处以自定义监控脚本为例来进行配置监控

2、修改agent的配置文件

[[email protected] port_6190]# vi /usr/local/zabbix/etc/zabbix_agentd.conf

[[email protected] port_6190]# cat /usr/local/zabbix/etc/zabbix_agentd.conf|grep -v '^#'|grep -v '^$'
LogFile=/data/zabbix_agent/zabbix_agentd.log
UnsafeUserParameters=1                   
Include= /usr/local/zabbix/etc/zabbix_agentd.conf.d/
Server=192.168.94.78
ServerActive=192.168.94.78
Hostname=test
 UserParameter=com_writes,/soft/port_6190/mysql_status.pl |grep com_writes|awk -F '=' '{print $2}'
 UserParameter=com_reads,/soft/port_6190/mysql_status.pl  |grep com_reads |awk -F '=' '{print $2}'
 UserParameter=com_update,/soft/port_6190/mysql_status.pl |grep com_update|awk -F '=' '{print $2}'
 UserParameter=com_insert,/soft/port_6190/mysql_status.pl |grep com_insert|awk -F '=' '{print $2}'
 UserParameter=com_delete,/soft/port_6190/mysql_status.pl |grep com_delete|awk -F '=' '{print $2}'

注:com_delet 为键值名,后面为键值的值

重启agent

/etc/init.d/zabbix_agentd stop
/usr/local/zabbix/sbin/zabbix_agentd -c /usr/local/zabbix/etc/zabbix_agentd.conf

3、server端检查

检查

[[email protected] bin]# pwd
/usr/local/zabbix/bin

[[email protected] bin]# ./zabbix_get -s 192.168.94.144 -k com_writes
0

4、server端进行界面配

4.1添加host

未分类

4.2 添加模版

未分类

4.3 创建应用集

未分类

未分类

4.4创建项目

未分类

4.5 创建图形

未分类

未分类

未分类

5、查看监控图

未分类

6、其他过程

未分类

未分类

未分类

未分类

未分类

未分类

未分类

未分类

未分类

未分类