您的位置:首页 > 运维架构 > Shell

一个精简的nginx日志切割shell

2013-01-29 09:44 399 查看
LAMP环境是基础
Rrdtool是绘图工具
可以yum安装、也可以源码包安装
Rrdtool安装需要cairo、libxml2、pango库支持,可通过yum安装
[root@cacti ~]# yum -y install cairo-devel libxml2-develpango-devel
[root@cacti ~]# tar zxvf rrdtool-1.4.7.tar.tar
[root@cacti ~]# cd rrdtool-1.4.7
[root@cacti rrdtool-1.4.7]# ./configure--prefix=/usr/local/rrdtool
[root@cacti rrdtool-1.4.7]# make && make install
[root@cacti rrdtool-1.4.7]# ln -s/usr/local/rrdtool/bin/* /usr/local/bin/

Cacti安装
Tar fvxz cacti-0.8.7e.tar.gz
mv cacti-0.8.7e /var/www/html/cacti
chown -R apache:apache/var/www/html/cacti
mysql -uroot
mysql> create database cacti;
mysql> grant all privileges on cacti.* tocacti@'localhost' identified by 'cacti123';
mysql> flush privileges;

cd /var/www/html/cacti
mysql -ucacti -pcacti123 cacti<cacti.sql #将cacti数据库导入
修改网站配置文件,使用正确连接数据库参数
vim include/config.php
$database_type = "mysql";
$database_default = "cacti";
$database_hostname = "localhost";
$database_username = "cacti";
$database_password = "cacti123";
$database_port = "3306";

Chmod 777 -R /var/www/html/cacti/rra(存放绘制的图)
Chmod 777 -R /var/www/html/cacti/log
修改计划任务
*/2 * * * * php /var/www/html/cacti/poller.php >/dev/null 2>&1

使用浏览器打开http://192.168.209.130/cacti/install/
默认用户名和密码都是cacti,登陆后会要求修改

监控远程主机,需要在被监控端安装snmp服务
Wget http://jaist.dl.sourceforge.net/project/net-snmp/net-snmp/5.4.4/net-snmp-5.4.4.tar.gz
1.tar -xzvfnet-snmp-5.4.1.tar.gz//解压
2.cd /net-snmp-5.4.4
3../configure --prefix=/usr/local/net-snmp --with-default-snmp-version="2"--with-sys-contact="lgao@cti.com.cn" --with-sys-location="china"--with-logfile="/var/log/snmpd.log"--with-persistent-directory="/var/net-snmp"
prefix:net-snmp将要安装的路径
enable-mfd-rewrites:允许用新的MFD重写可用的mid模块
with-default-snmp-version:默认的SNMP版本
with-sys-contact:可以配置该设备的联系人
with-sys-location:该设备的位置
with-logfile:日志文件路径
with-persistent-directory:不变数据存储目录
4.make
5.make install
6.cd/usr/local/net-snmp/share/snmp
7.配置snmpd.conf(原本不是不存在的)
a)/usr/local/net-snmp/bin/snmpconf
Do youwant to configure the information returned in the system MIB group (contactinfo, etc)? (default = y): y
Thelocation of the system: At home
Thecontact information: SirDice
does thishost offer physical services (eg, like a repeater) [answer 0 or 1]: 0
does this host offer datalink/subnetworkservices (eg, like a bridge): 0
does this host offer internet services (eg,supports IP): 1
does this host offer end-to-end services (eg,supports TCP): 1
does this host offer application services (eg,supports SMTP): 1
Do youwant to configure the agent's access control? (default = y): y
Do you want to allow SNMPv3 read-write userbased access (default = y): n
Do you want to allow SNMPv3 read-only userbased access (default = y): n
Do you want to allow SNMPv1/v2c read-writecommunity access (default = y): n
Do you want to allow SNMPv1/v2c read-onlycommunity access (default = y): y
Thehostname or network address to accept this community name from [RETURN forall]:
The OID that this community should berestricted to [RETURN for no-restriction]:
Do anotherrocommunity line? (default = y): n
Do youwant to configure where and if the agent will send traps? (default = y): n
Do youwant to configure the agent's ability to monitor various aspects of yoursystem? (default = y): n

cp snmpd.conf /usr/local/share/snmp/

b)如果这样的配置文件不能成功启动,请手写
cat /usr/local/net-snmp/share/snmp/snmpd.conf

com2sec notConfigUser  127.0.0.1      public
com2sec notConfigUser  10.8.8.141      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)
pass .1.3.6.1.4.1.4413.4.1 /usr/bin/ucd5820stat
启动:/usr/local/net-snmp/sbin/snmpd -c/usr/local/net-snmp/share/snmp/snmpd.conf
检查:ps –ef | grep snmp或者:netstat –nutlp | grep:166
测试: /usr/local/net-snmp/bin/snmpwalk -c public -v 2c 127.0.0.1有输出,则完成
注意防火墙

被监控主机安装snmp服务之后,就可以在cacti的web控制台上来配置监控了!
也可以在cacti 服务器上来操作
cat cacti.sh
#!/bin/bash
#Create by yuchunyun 2014/03/14
#This shell be used for automatic add client for Cacti
#You should make sure the client had open snmp port for you!#
cacti_path=/var/www/html/cacti
read -p "please input the hostip you want monitor:" host_ip
read -p "please input host:$host_ip SNMP Community(public):" communtiy
snmpwalk -v 2c -c $communtiy $host_ip ifDescr #try to contect client snmp #
if [ $? -eq 0 ];then
#添加被监控端
php $cacti_path/cli/add_device.php --description="$host_ip" --ip="$host_ip" --template=3 --version=2 --community="$communtiy"
#获取刚刚添加的主机的id,下面要用此id
host_id=`php /var/www/html/cacti/cli/add_graphs.php --list-hosts | grep $host_ip | awk -F " " '{print $1}'`
#添加被监控主机的模块
for ((i=0;i<10;i++));
do
echo "###The following is a list of some commonly used###
ID     Graph Templates
1      quit!
4      ucd/net - CPU Usage              ****#CPU使用情况
7      Unix - Ping Latency      ****#ping的延迟情况
8      Unix - Processes         ****#进程数
9      Unix - Load Average
10     Unix - Logged in Users       ****#登陆用户
11     ucd/net - Load Average       ****#系统负载
12     Linux - Memory Usage
13     ucd/net - Memory Usage       ****#内存使用情况
14     Netware - File System Cache
15     Netware - CPU Utilization
16     Netware - File System Activity
17     Netware - Logged In Users
18     Cisco - CPU Usage
20     Netware - Directory Information
21     Unix - Available Disk Space
22     Interface - Errors/Discards
23     Interface - Unicast Packets
24     Interface - Non-Unicast Packets
26     Host MIB - Available Disk Space
33     Interface - Traffic (bytes/sec, Total Bandwidth)
"
read -p "please choose some templates to monitor and input them ID:" template_id
if [ $template_id -ne 1 ];then
php $cacti_path/cli/add_graphs.php --host-id="$host_id" --graph-type=cg --graph-template-id="$template_id"
else
break
fi
done
#添加对网卡的监控
php $cacti_path/cli/add_graphs.php --host-id=$host_id --graph-type=ds  --graph-template-id=33 --snmp-query-id=1 --snmp-query-type-id=16 --snmp-field=ifDescr --snmp-value="eth0"
#添加对磁盘使用的监控
for ((i=0;i<2;i++));
do
read -p "please input the parttition you want monitor(/|/boot)" parttition
if [ "$parttition" = "/" ] || [ "$parttition" = "/boot" ];then
php $cacti_path/cli/add_graphs.php --host-id="$host_id" --graph-type=ds  --graph-template-id=26 --snmp-query-id=8 --snmp-query-type-id=18 --snmp-field=hrStorageDescr --snmp-value="$parttition"
else
echo "your input is invalid!"
fi
done
#将主机加入到树中
php $cacti_path/cli/add_tree.php --list-trees
read -p "please input tree_ID:" tree
php $cacti_path/cli/add_tree.php --host-id=$host_id --type=node --node-type=host --tree-id=$tree
else
echo "contect $host_ip snmp faild!"
fi


Cacti的优化

1、让Cacti使用Spine插件,并进行相关设置
关于spine插件的安装,这里就不讲了
访问:http://cacti_host_ipaddress/cacti
Settings -> Paths -> "Spine Poller File Path" -> spine程序的位置
Settings -> Poller -> "Poller Type" -> spine

2、进程调整

对于进程和线程的调整有利于缩短采集时间,可以进行大量的采集。在CU论坛上看有人说cacti能够同时采集1k服务,这样的性能没有测试过,但是有人测试过同时采集200台服务器的,占用的内容只有几百兆,cpu的利用率也很低,说明一台的普通的机器排除网络链路延时采集的性能是相当强悍的

Settings -> Poller -> "Maximum Concurrent Poller Processes"(最大并发轮询器进程),修改该值
Settings -> Poller -> "Maximum Threads per Process"(每进程最大线程数),修改该值
Settings -> Poller -> "Number of PHP Script Servers"(PHP脚本服务程序数),修改该值
通过修改这三个参数,可以提高一部分的性能

3、增加php 的内存使用的上限值
# vi /etc/php.ini
memory_limit = 256M (预设是128MB,根据内存的使用情况,可以改大一点)

4、定期 optimize cacti 数据库
cacti 的 poller_output 会不明原因的长大,大到数据越写越慢,直到无法更新。每隔一段时间手动 “Truncate poller_output table” 后,就会继续正常运作;所以我们可以利用crontab定期清除数据
#vi /etc/cron.d/truncate_poller
0 0 * * 0 root /usr/local/mysql/bin/mysql --user=cacti --password=2010 cacti -e ‘truncate table poller_output;’
#chkconfig --level 3 crond on && service crond start

5、修改mysql的最大连接数

一般默认安装的时候没有调整最大连接数的话,当需要并发量的时候可以出图就会断断续续的
mysql> set GLOBAL max_connections=200000;
或者直接修改my.cnf配置文件,增加:max_connections = 200000;

出于效率原因,在大量采集数据时使用自带的cmd.php轮询器会比较慢,1分钟1次的采集频率可能无法完成轮询所有机器。这里为了优化,采用官方推荐的spine来高效轮询。本例使用源代码编译安装,下载的版本是spine-0.8.8a,所用cacti为0.8.7e
# cd /home/yuchunyun
# wget http://www.cacti.net/downloads/spine/cacti-spine-0.8.8a.tar.gz
# tar -xvf cacti-spine-0.8.8a.tar.gz
#cd cacti-spine-0.8.8a# ./configure --prefix=/usr/local/spine注:如果报错configure: error: Cannot find SNMP headers. Use --with-snmp= to specify non-default path. sudo yum install net-snmp-devel -y#sudo make#sudo make install
2.修改访问数据库的用户名密码信息#cp /usr/local/spine/etc/spine.conf.dist /usr/local/spine/etc/spine.conf
# vi /usr/local/spine/etc/spine.conf 修改数据库连接所用到的host,db,user,passwd。要与/var/www/html/cacti/include/config.php保持一致!
3.测试是否正常
# /usr/local/spine/bin/spineSPINE: Using spine config file [spine.conf]
SPINE: Version 0.8.8a starting
SPINE: Time: 5.6360 s, Threads: 5, Hosts: 22
(如果提示找不到配置文件,可以加-C选项手动指定配置文件路径)
4.在cacti里设置spine路径
Console -> Configuration -> Settings -> Paths -> Alternate Poller Path -> Spine Poller File Path
/usr/local/spine/bin/spine
5. 修改Cacti使用的Poller Type
Console -> Cacti Settings->Poller->Poller Type
在下拉中选择spine
===================================================================================以下线程调整引用自http://www.chenjunlu.com/2011/05/tips-for-improving-the-performance-of-cacti-server/ 修改 Spine Specific Execution Parameters 里的 Maximum Threads per Process 为监控服务器 cpu 个数的2倍。我在实际的操作中将该值从默认值“1”修改为“16”,但是 Poller 的运行时间反倒增加了,再修改为“8”,情况好一些,所以大家可以自己随意改写该数值,然后在 cacti.log 文件中查询 Poller 轮询运行时间的变化,然后确定最优的方案。===================================================================================
实际测试,21台机器,4个线程,4秒左右轮询一遍,而采用cmd.php的时候基本需要14.6~15.2秒。提升效果还是很明显的。

解决cacti大内存出现数据为nan

今天查看监控,发现memory usage哪里不对劲,仔细一看,原来Cache Memory的图不见了,buffers和free的图还在,其余一切正常,google一下,发现这是由于rrdtools默认的内存上限为10G的原因造成的,问题以解决,分享一下解决方法:

网上的此问题大多数是出现在memory free不出图,修改*_mem_free*.rrd,我试过之后,等了一会发现没效果,仔细一看,我的问题出现在memory cache上,我修改错了文件了,当然没效果,后来修改自己的*_memory_cache*.rrd,果然有效果了,但仔细一想,这方法治标不治本,如果哪天把rrd文件删了,新生成的rrd上限还是10G,所以就想到了模版,以下整理出来的完整解决方法:
1.修改模版
Console—〉Data Templates中有三个关于memory的模版文件,网上一般只改free,其实我们改的是上限,不影响别的,所以把三个都改了也没关系,因为如果内存足够大,你buffers一释放,free自然就大了,超过了限定值就不出图,多闹心,所以我直接把三个都改了,这样不管哪个增大,都不会超过上限,,依次点击每个关于memory的模版,修改里面的Maximum Value 值,默认10G,加个0,变成100G了,依次保存,ok!
2.修改以生成的rra文件
#rrdtool info *_mem_cache_*.rrd (*值不固定,根据自己的来),到里面看,ds[mem_cache].max = 1.0000000000e+07 一项都是10G,所以我们要改这里(另外两个也同理)
# rrdtool tune *_mem_free_*.rrd -a mem_free:100000000
# rrdtool tune *_mem_buffers_*.rrd -a mem_buffers:100000000
# rrdtool tune *_mem_cache_*.rrd -a mem_cache:100000000

修改之后,稍等片刻,就出图了,之前为nan的数据是不会出现的,出现的是后面新生成的数据,不管是你的free不出图,还是cache不出图,这回都该出来了,ok,搞定收工.

本文出自 “月满轩尼诗” 博客,请务必保留此出处http://sunnyyu.blog.51cto.com/8745931/1401293
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: