海运的博客

Linux下用inotify-tool实时监控服务器文件系统

发布时间:May 20, 2012 // 分类:备份存储 // No Comments

inotify-tools是linux下通过inotify机制监控文件变化的命令行工具,可实时监控服务器文件变化并记录,如果服务器出现问题可协助查找原因,安装要求内核大于2.6.13。
查看服务器是否符合安装要求:

uname -a
Linux centos5.7-x64 2.6.18-274.17.1.el5 
ll /proc/sys/fs/inotify/
-rw-r--r-- 1 root root 0 05-15 01:12 max_queued_events
-rw-r--r-- 1 root root 0 05-15 01:12 max_user_instances
-rw-r--r-- 1 root root 0 05-15 01:12 max_user_watches

服务器如有安装EPEL源可使用yum安装:

yum install inotify-tools

编译安装:

cd /usr/local/src/
wget --no-check-certificate https://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar zxvf inotify-tools-3.14.tar.gz 
cd inotify-tools-3.14
./configure 
make
make install
ldconfig -v

inotify相关参数:

/proc/sys/fs/inotify/max_queued_events  #请求events数的最大值
/proc/sys/fs/inotify/max_user_instances #每个user可创建的instances数量上限
/proc/sys/fs/inotify/max_user_watches   #可监控的目录最大数

inotifywait是一个监控等待事件,可配合shell实时监控记录文件系统,常用参数:

--timefmt 时间格式
%y年 %m月 %d日 %H小时 %M分钟
--format 输出格式
%T时间 %w路径 %f文件名 %e状态
-m 始终保持监听状态,默认触发事件即退出。
-r 递归查询目录
-q 打印出监控事件
-e 定义监控的事件,可用参数:
open   打开文件
access 访问文件
modify 修改文件
delete 删除文件
create 新建文件
attrb  属性变更

应用示例:
实时监控wwwroot目录下新建或修改的文件并输出:

inotifywait -mrq -e modify,create --timefmt '%y-%m-%d %H:%M' --format '%T %f %e' /home/wwwroot/

输出如下:

12-05-14 20:43 favicon.ico
12-05-14 20:43 favicon.ico
12-05-14 20:43 tab_console_down.gif
12-05-14 20:43 tab_console_down.gif
12-05-14 20:43 tab_graphs.gif
12-05-14 20:43 tab_graphs.gif

Awk/Sed中调用shell变量方法

发布时间:May 19, 2012 // 分类:Shell // No Comments

Awk和Sed是Linux下很强大的两个文件处理工具,在编写Shell的时候经常要调用变量,记录调用方法。
1.Awk中调用方法:
1.1.使用"'$var'"方式:

#/bin/bash
num=1
awk -F: '{print $"'$num'"}' /etc/passwd

1.2.通过awk -v参数定义:

#!/bin/bash 
num=1
awk -v a=$num -F: '{print $a}' /etc/passwd

2.Sed中调用方法:

#!/bin/bash
#https://www.haiyun.me
for ((i=1; i<25; i++))
do
        ip=`sed -n "$i"p ip.txt`
        sed -i ''"$i"' s/$/ '"$ip"'/' ip-mac.txt
done

PHP动态编译添加snmp模块

发布时间:May 19, 2012 // 分类:PHP // No Comments

因程序原因需临时添加snmp支持,不想重新编译php,可动态编译添加snmp模块。
进入PHP源码扩展目录:

cd php-5.2.17/ext/snmp
/usr/local/php/bin/phpize 
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install

加载模块:

cat >> /usr/local/php/etc/php.ini <<EOF
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"
extension=snmp.so
EOF

重新加载配置文件,查看snmp.so是否加载。

php -m

Apache配置301重定向

发布时间:May 19, 2012 // 分类:Apache // No Comments

网站目录或网页出现变动要使用301重定向转移权重,有时域名也要将www.haiyun.me重定向到www.haiyun.me,本文记录下Apaceh下301重定向方法,Nginx下301重定向请参考:https://www.haiyun.me/archives/nginx-301.html
1.使用RedirectMatch和Redirect重定向,这两个命令使用方法相似,RedirectMatch支持正则表达式,可以批量重定向。
使用语法:

Redirect [status] URL URL
RedirectMatch [status] regex URL

[status]可用参数:

permanent 返回永久性重定向状态码(301),表示此资源的链接变动是永久性的。
temp 返回临时性重定向状态码(302),默认值。
seeother 返回一状态码(303),表示此资源已经被替代。
gone 返回状态码(410),表示此资源已经被永久性地删除了。如果指定了这个状态码,则URL参数将被忽略。

应用示例:

Redirect permanent /index.html https://www.haiyun.me/index.php
RedirectMatch 301 (.*).gif$ https://www.haiyun.me/$1.jpg

2.使用URL重写模块mod_rewrite重定向

RewriteEngine on
rewritecond %{http_host} ^www.haiyun.me [nc] #定义主机名变量,匹配执行下条规则。
rewriterule ^/(.*)$ https://www.haiyun.me/$1 [r=301,nc] #301重定向到www.haiyun.me,nc不区分大小写。

SNMP管理工具之snmpnetstat查看服务器网络状态

发布时间:May 19, 2012 // 分类:系统监控 // No Comments

snmpnetstat类似于netstat命令,用于通过snmp协议查看远程服务器网络状态。
使用语法:

snmpnetstat [common options] [-Ca] [-Cn] [-Cs] [-Cp protocol] AGENT

常用参数:

-Ca 输出所有连接
-Ci 输出所有网络接口统计数据
-CI 输出指定网络接口统计数据
-Cn IP地址方式输出
-Cp 输出TCP协议
-Cs 输出各种协议统计数据
-Cr 输出路由表

应用示例:
查看远程服务器所有TCP连接:

snmpnetstat -v 2c -c public -Canp tcp  www.haiyun.me

输出结果:

Active Internet (tcp) Connections (including servers)
Proto Local Address          Remote Address         (state)
tcp   *.22                   *.*                   LISTEN
tcp   *.80                   *.*                   LISTEN
tcp   *.111                  *.*                   LISTEN
tcp   *.443                  *.*                   LISTEN
tcp   *.3306                 *.*                   LISTEN
tcp   127.0.0.1.25           *.*                   LISTEN
tcp   127.0.0.1.199          *.*                   LISTEN
tcp   192.168.1.2.22         192.168.1.16.3817     ESTABLISHED
tcp   192.168.1.2.22         192.168.1.16.23730    ESTABLISHED
分类
最新文章
最近回复
  • 海运: 恩山有很多。
  • swsend: 大佬可以分享一下固件吗,谢谢。
  • Jimmy: 方法一 nghtp3步骤需要改成如下才能编译成功: git clone https://git...
  • 海运: 地址格式和udpxy一样,udpxy和msd_lite能用这个就能用。
  • 1: 怎么用 编译后的程序在家里路由器内任意一台设备上运行就可以吗?比如笔记本电脑 m参数是笔记本的...
  • 孤狼: ups_status_set: seems that UPS [BK650M2-CH] is ...
  • 孤狼: 擦。。。。apcupsd会失联 nut在冲到到100的时候会ONBATT进入关机状态,我想想办...
  • 海运: 网络,找到相应的url编辑重发请求,firefox有此功能,其它未知。
  • knetxp: 用浏览器F12网络拦截或监听后编辑重发请求,修改url中的set为set_super,将POS...
  • Albert: 啊啊啊啊啊啊啊啊啊 我太激动了,终于好了英文区搜索了半天,翻遍了 pve 论坛没找到好方法,博...