This article is focusing installing and configure MRTG with CPU,Memory and Disk Usage Graphs examples for Debian Ubuntu and Kubuntu Users and may be work for some debian based distributions.
MRTG is Multi Router Traffic Grapher (MRTG) is a tool to monitor the traffic load on network links. MRTG generates HTML pages containing PNG images which provide a LIVE visual representation of this traffic.
MRTG Requirements
Apache webserver with perl support
NET-SNMP
GD
Libpng
zlib
If you want to download MRTG you can download from here
Preparing you System for MRTG Instalaltion
First you need to install the required compilers
#apt-get install gcc make g++
Apache 2 Installation with perl support
Follow these instructions to install apache2 with perl support
MRTG Installation
Now we need to install mrtg and snmp
Installing MRTG in Debian, Ubuntu and Kubuntu
#apt-get install mrtg snmpd
The installation will create an mrtg subdirectory where the Apache Web pages reside. On your Debian,ubnutu,kubuntu systems the path of this subdirectory is:
/var/www/mrtg
Now you need to edit the mrtg configuration file to edit the some of the settings
File is located at /etc/mrtg.cfg you need to change the global settings as follows
# Global Settings
RunAsDaemon: yes
EnableIPv6: no
WorkDir: /var/www/mrtg
Options[_]: bits,growright
WriteExpires: Yes
Title[^]: Traffic Analysis for
You will find a crontab running every 5 minutes as user root
# cat /etc/cron.d/mrtg
0-55/5 * * * * root if [ -x /usr/bin/mrtg ] && [ -r /etc/mrtg.cfg ]; then env LANG=C /usr/bin/mrtg /etc/mrtg.cfg >> /var/log/mrtg/mrtg.log 2>&1; fi
Now we need to assign the snmp community name in snmp configration file /etc/snmp/snmpd.conf
# sec.name source community
# com2sec paranoid default public
com2sec readonly default public
#com2sec readwrite default private
Now you need to restart the snmp service
#/etc/init.d/snmpd restart
The configuration file creating using
#cfgmaker public@localhost > /etc/mrtg.cfg
Creating a configuration file for a device using cfgmaker
#cfgmaker public@192.168.0.1 >> /etc/mrtg.cfg
With the configuration file created correctly there’s only one other thing you have to do and that’s to use the indexmaker utility to create the summary home page. Since you have to re-run this command every time you make certain changes to the /etc/mrtg.cfg configuration file.
Creating index file for the webserver using indexmaker
#indexmaker /etc/mrtg.cfg > /var/www/mrtg/index.html
Now you need to reboot your system wait for five minutes or so and then take a look at your summary home page. If your Debian,ubuntu,kubuntu system’s IP address is 192.168.0.1 then you’d type in the following in the address bar of a browser running on a system on the same network:
http://192.168.0.1/mrtg/
Your summary home page should come up with a graph for each target entry in the configuration file. If a graph looks like there’s no data on it, click on it and check the statistics to see if any traffic is being seen. Small amounts of traffic won’t show up on the graphs because we used the Unscaled statement
Some of examples how to monitor cpu , memory , Disk usage
CPU Usage
/etc/mrtg/cpu.cfg
WorkDir: /var/www/mrtg
LoadMIBs: /usr/share/snmp/mibs/UCD-SNMP-MIB.txt
Target[localhost.cpu]:ssCpuRawUser.0&ssCpuRawUser.0:public@127.0.0.1+ ssCpuRawSystem.0&ssCpuRawSystem.0:public@127.0.0.1+
ssCpuRawNice.0&ssCpuRawNice.0:public@127.0.0.1
RouterUptime[localhost.cpu]: public@127.0.0.1
MaxBytes[localhost.cpu]: 100
Title[localhost.cpu]: CPU Load
PageTop[localhost.cpu]: Active CPU Load %
Unscaled[localhost.cpu]: ymwd
ShortLegend[localhost.cpu]: %
YLegend[localhost.cpu]: CPU Utilization
Legend1[localhost.cpu]: Active CPU in % (Load)
Legend2[localhost.cpu]:
Legend3[localhost.cpu]:
Legend4[localhost.cpu]:
LegendI[localhost.cpu]: Active
LegendO[localhost.cpu]:
Options[localhost.cpu]: growright,nopercent
Memory Usage
/etc/mrtg/mem.cfg
LoadMIBs: /usr/share/snmp/mibs/HOST-RESOURCES-MIB.txt
Target[localhost.mem]: .1.3.6.1.4.1.2021.4.6.0&.1.3.6.1.4.1.2021.4.6.0:public@localhost
PageTop[localhost.mem]:Free Memory
WorkDir: /var/www/mrtg
Options[localhost.mem]: nopercent,growright,gauge,noinfo
Title[localhost.mem]: Free Memory
MaxBytes[localhost.mem]: 1000000
kMG[localhost.mem]: k,M,G,T,P,X
YLegend[localhost.mem]: bytes
ShortLegend[localhost.mem]: bytes
LegendI[localhost.mem]: Free Memory:
LegendO[localhost.mem]:
Legend1[localhost.mem]: Free memory, not including swap, in bytes
Memory Monitoring (Total Versus Available Memory)
/etc/mrtg/memfree.cfg
LoadMIBs: /usr/share/snmp/mibs/HOST-RESOURCES-MIB.txt
Target[server.memory]: memAvailReal.0&memTotalReal.0:public@localhost
Title[server.memory]: Free Memory
PageTop[server.memory]: < H1 >Free Memory< /H1 >
MaxBytes[server.memory]: 100000000000
ShortLegend[server.memory]: B
YLegend[server.memory]: Bytes
LegendI[server.memory]: Free
LegendO[server.memory]: Total
Legend1[server.memory]: Free memory, not including swap, in bytes
Legend2[server.memory]: Total memory
Options[server.memory]: gauge,growright,nopercent
kMG[server.memory]: k,M,G,T,P,X
Memory Monitoring (Percentage usage)
/etc/mrtg/mempercent.cfg
LoadMIBs: /usr/share/snmp/mibs/HOST-RESOURCES-MIB.txt
Title[server.mempercent]: Percentage Free Memory
PageTop[server.mempercent]: < H1 >Percentage Free Memory< /H1 >
Target[server.mempercent]: ( memAvailReal.0&memAvailReal.0:publicy@localhost ) * 100 / ( memTotalReal.0&memTotalReal.0:public@localhost )
options[server.mempercent]: growright,gauge,transparent,nopercent
Unscaled[server.mempercent]: ymwd
MaxBytes[server.mempercent]: 100
YLegend[server.mempercent]: Memory %
ShortLegend[server.mempercent]: Percent
LegendI[server.mempercent]: Free
LegendO[server.mempercent]: Free
Legend1[server.mempercent]: Percentage Free Memory
Legend2[server.mempercent]: Percentage Free Memory
Disk Usage
/etc/mrtg/disk.cfg
LoadMIBs: /usr/share/snmp/mibs/HOST-RESOURCES-MIB.txt
Target[server.disk]: dskPercent.1&dskPercent.2:public@localhost
Title[server.disk]: Disk Partition Usage
PageTop[server.disk]: < H1 >Disk Partition Usage /home and /var< /H1 >
MaxBytes[server.disk]: 100
ShortLegend[server.disk]: %
YLegend[server.disk]: Utilization
LegendI[server.disk]: /home
LegendO[server.disk]: /var
Options[server.disk]: gauge,growright,nopercent
Unscaled[server.disk]: ymwd
Creating jobs for CPU , Memory and Disk Usage
CPU
/etc/cron.mrtg/cpu
#!/bin/sh
/usr/bin/mrtg /etc/mrtg/cpu.cfg
Memory
/etc/cron.mrtg/mem
#!/bin/sh
/usr/bin/mrtg /etc/mrtg/mem.cfg
Memory Free
/etc/cron.mrtg/memfree
#!/bin/sh
/usr/bin/mrtg /etc/mrtg/memfree.cfg
Memory Percentage
/etc/cron.mrtg/mempercent
#!/bin/sh
/usr/bin/mrtg /etc/mrtg/mempercent.cfg
Disk
/etc/cron.mrtg/disk
#!/bin/sh
/usr/bin/mrtg /etc/mrtg/disk.cfg
Run each script 3 times (disregard the warnings)
/etc/cron.mrtg/cpu
/etc/cron.mrtg/mem
/etc/cron.mrtg/memfree
/etc/cron.mrtg/mempercent
/etc/cron.mrtg/disk
Make the Index Files
#/usr/bin/indexmaker --output=/var/www/mrtg/index.html \
--title=”Memory and CPU Usage ” \
--sort=name \
--enumerate \
/etc/mrtg/cpu.cfg \
/etc/mrtg/mem.cfg \
/etc/cron.mrtg/memfree \
/etc/cron.mrtg/mempercent \
/etc/cron.mrtg/disk
Make the mrtg.cfg file
#cfgmaker --global “WorkDir: /var/www/mrtg/” \
--global “Options[_]: growright,bits” \
--ifref=ip \
public@localhost > /etc/mrtg/mrtg.conf
Cronjob setup
/bin/cat >> /var/spool/cron/crontabs/root
*/5 * * * * /bin/run-parts /etc/cron.mrtg 1> /dev/null
Now you logon to your web browser http://192.168.0.1/mrtg/ and Now you should see CPU,Memory and Disk Usage graphs.
If you want more documentation about MRTG check here
1 TOP
这是一个大家经常问到的问题,例如在SQLSERVER中可以使用如下语句来取得记录集中的前十条记录:
SELECT TOP 10 * FROM [index] ORDER BY indexid DESC;
但是这条SQL语句在SQLite中是无法执行的,应该改为:
SELECT * FROM [index] ORDER BY indexid DESC limit 0,10;
其中limit 0,10表示从第0条记录开始,往后一共读取10条
2 创建视图(Create View)
SQLite在创建多表视图的时候有一个BUG,问题如下:
CREATE VIEW watch_single AS SELECT DISTINCTwatch_item.[watchid],watch_item.[itemid] FROM watch_item;
上面这条SQL语句执行后会显示成功,但是实际上除了
SELECT COUNT(*) FROM [watch_single ] WHERE watch_ single.watchid = 1;
能执行之外是无法执行其他任何语句的。其原因在于建立视图的时候指定了字段所在的表名,而SQLite并不能正确地识别它。所以上面的创建语句要改为:
CREATE VIEW watch_single AS SELECT DISTINCT [watchid],[itemid] FROM watch_item;
但是随之而来的问题是如果是多表的视图,且表间有重名字段的时候该怎么办?
3 COUNT(DISTINCT column)
SQLite在执行如下语句的时候会报错:
SELECT COUNT(DISTINCT watchid) FROM [watch_item] WHERE watch_item.watchid = 1;
其原因是SQLite的所有内置函数都不支持DISTINCT限定,所以如果要统计不重复的记录数的时候会出现一些麻烦。比较可行的做法是先建立一个不重复的记录表的视图,然后再对该视图进行计数。
4 外连接
虽然SQLite官方已经声称LEFT OUTER JOIN 已经实现,但还没有 RIGHT OUTER JOIN 和 FULL OUTER JOIN。但是实际测试表明似乎并不能够正常的工作。以下三条语句在执行的时候均会报错:
SELECT tags.[tagid] FROM [tags],[tag_rss] WHERE tags.[tagid] = tag_rss.[tagid](*);
SELECT tags.[tagid] FROM [tags],[tag_rss] WHERE LEFT OUTER JOIN tag_rss.[tagid] = tags.[tagid];
SELECT tags.[tagid] FROM [tags],[tag_rss] WHERE LEFT JOIN tag_rss.[tagid] = tags.[tagid];
此外经过测试用+号代替*号也是不可行的。
收集SQLite与Sql Server的语法差异
1.返回最后插入的标识值
返回最后插入的标识值sql server用@@IDENTITY
sqlite用标量函数LAST_INSERT_ROWID()
返回通过当前的 SQLConnection 插入到数据库的最后一行的行标识符(生成的主键)。此值与 SQLConnection.lastInsertRowID 属性返回的值相同。
2.top n
在sql server中返回前2行可以这样:
select top 2 * from aa
order by ids desc
sqlite中用LIMIT,语句如下:
select * from aa
order by ids desc
LIMIT 2
3.GETDATE ( )
在sql server中GETDATE ( )返回当前系统日期和时间
sqlite中没有
4.EXISTS语句
sql server中判断插入(不存在ids=5的就插入)
IF NOT EXISTS (select * from aa where ids=5)
BEGIN
insert into aa(nickname)
select 't'
END
在sqlite中可以这样
insert into aa(nickname)
select 't'
where not exists(select * from aa where ids=5)
5.嵌套事务
sqlite仅允许单个活动的事务
6.RIGHT 和 FULL OUTER JOIN
sqlite不支持 RIGHT OUTER JOIN 或 FULL OUTER JOIN
7.可更新的视图
sqlite视图是只读的。不能对视图执行 DELETE、INSERT 或 UPDATE 语句,sql server是可以对视图 DELETE、INSERT 或 UPDATE
一、 LVS简介
LVS是Linux Virtual Server的简称,也就是Linux虚拟服务器, 是一个由章文嵩博士发起的自由软件项目,它的官方站点是www.linuxvirtualserver.org。现在LVS已经是 Linux标准内核的一部分,在Linux2.4内核以前,使用LVS时必须要重新编译内核以支持LVS功能模块,但是从Linux2.4内核以后,已经完全内置了LVS的各个功能模块,无需给内核打任何补丁,可以直接使用LVS提供的各种功能。使用LVS技术要达到的目标是:通过LVS提供的负载均衡技术和Linux操作系统实现一个高性能、高可用的服务器群集,它具有良好可靠性、可扩展性和可操作性。从而以低廉的成本实现最优的服务性能。
关于LVS的安装与介绍,在前面的文章中已经有过深入介绍,这里不再讲述。
本文讲解的环境如下:
操作系统:统一采用Centos5.3版本,地址规划如下:
图1
图1中的VIP指的是虚拟IP地址,还可以叫做LVS集群的服务IP,在DR、TUN模式中,数据包是直接返回给用户的,所以,在Director Server上以及集群的每个节点上都需要设置这个地址。此IP在Real Server上一般绑定在回环地址上,例如lo:0,同样,在Director Server上,虚拟IP绑定在真实的网络接口设备上,例如eth0:0。
各个Real Server可以是在同一个网段内,也可以是相互独立的网段,还可以是分布在internet上的多个服务器.
LVS+Keepalived高可用负载均衡集群拓扑结构如图2所示:
图2
二、安装Keepalived
keepalived起初是为LVS设计的,专门用来监控集群系统中各个服务节点的状态,后来又加入了VRRP的功能,VRRP是Virtual Router Redundancy Protocol(虚拟路由器冗余协议)的缩写,VRRP出现的目的就是为了解决静态路由出现的单点故障问题,它能够保证网络的不间断、稳定的运行。所以,keepalived一方面具有服务器健康检测功能,另一方面也具有HA cluster功能。
Keepalived的官方站点是http://www.keepalived.org,可以在这里下载到各种版本,我们这里下载的是keepalived-1.1.19.tar.gz,安装步骤如下:
[root@DR1 ~]#tar zxvf keepalived-1.1.19.tar.gz
[root@DR1 ~]#cd keepalived-1.1.19
[root@DR1 keepalived-1.1.19]#./configure --sysconf=/etc \
> --with-kernel-dir=/usr/src/kernels/2.6.18-8.el5-i686
[root@DR1 keepalived-1.1.19]#make
[root@DR1 keepalived-1.1.19]#make install
[root@DR1 keepalived-1.1.19]#ln -s /usr/local/sbin/keepalived /sbin/
在编译选项中,“--sysconf”指定了Keepalived配置文件的安装路径,即路径为/etc/Keepalived/Keepalived.conf,“--with-kernel-dir”这是个很重要的参数,但这个参数并不是要把Keepalived编译进内核,而是指定使用内核源码里面的头文件,就是include目录。如果要使用LVS时,才需要用到此参数,否则是不需要的.
三、配置Keepalived
Keepalived的配置非常简单,仅仅需要一个配置文件即可完成HA cluster和lvs服务节点监控功能,Keepalived的安装已经在上面章节进行了介绍,在通过Keepalived搭建高可用的LVS集群实例中,主、备Director Server都需要安装Keepalived软件,安装成功后,默认的配置文件路径为/etc/Keepalived/Keepalived.conf。一个完整的keepalived配置文件,有三个部分组成,分别是全局定义部分、vrrp实例定义部分以及虚拟服务器定义部分,下面详细介绍下这个配置文件中每个选项的详细含义和用法:
#全局定义部分
global_defs {
notification_email {
dba.gao@gmail.com #设置报警邮件地址,可以设置多个,每行一个。注意,如果要开启邮件报警,需要开启本机的sendmail服务。
ixdba@163.com
}
notification_email_from Keepalived@localhost #设置邮件的发送地址。
smtp_server 192.168.200.1 #设置smtp server地址。
smtp_connect_timeout 30 #设置连接smtp服务器超时时间。
router_id LVS_MASTER #运行Keepalived服务器的一个标识。发邮件时显示在邮件标题中的信息
}
#vrrp实例定义部分
vrrp_instance VI_1 {
state MASTER #指定Keepalived的角色,MASTER表示此主机是主用服务器,BACKUP表示是备用服务器。
interface eth0 #指定HA监测网络的接口。
virtual_router_id 51 #虚拟路由标识,这个标识是一个数字,并且同一个vrrp实例使用唯一的标识,即同一个vrrp_instance下,MASTER和BACKUP必须是一致的。
priority 100 #定义优先级,数字越大,优先级越高,在一个vrrp_instance下,MASTER的优先级必须大于BACKUP的优先级。
advert_int 1 #设定MASTER与BACKUP负载均衡器之间同步检查的时间间隔,单位是秒。
authentication { #设定验证类型和密码。
auth_type PASS #设置验证类型,主要有PASS和AH两种。
auth_pass 1111 #设置验证密码,在一个vrrp_instance下,MASTER与BACKUP必须使用相同的密码才能正常通信。
}
virtual_ipaddress { #设置虚拟IP地址,可以设置多个虚拟IP地址,每行一个。
192.168.12.135
}
}
#虚拟服务器定义部分
virtual_server 192.168.12.135 80 { #设置虚拟服务器,需要指定虚拟ip地址和服务端口,ip与端口之间用空格隔开。
delay_loop 6 #设置健康检查时间,单位是秒。
lb_algo rr #设置负载调度算法,这里设置为rr,即轮询算法。
lb_kind DR #设置LVS实现负载均衡的机制,可以有NAT、TUN和DR三个模式可选。
persistence_timeout 50 #会话保持时间,单位是秒,这个选项对于动态网页是非常有用的,为集群系统中session共享提供了一个很好的解决方案。有了这个会话保持功能,用户的请求会被一直分发到某个服务节点,直到超过这个会话保持时间。需要注意的是,这个会话保持时间,是最大无响应超时时间,也就是说用户在操作动态页面时,如果在50秒内没有执行任何操作,那么接下来的操作会被分发到另外节点,但是如果一直在操作动态页面,则不受50秒的时间限制。
protocol TCP #指定转发协议类型,有tcp和udp两种。
real_server 192.168.12.246 80 { #配置服务节点1,需要指定real server的真实IP地址和端口,ip与端口之间用空格隔开。
weight 3 #配置服务节点的权值,权值大小用数字表示,数字越大,权值越高,设置权值的大小可以为不同性能的服务器分配不同的负载,可以对性能高的服务器设置较高的权值,而对性能较低的服务器设置相对较低的权值,这样就合理的利用和分配了系统资源。
TCP_CHECK { #realserve的状态检测设置部分,单位是秒
connect_timeout 10 #10秒无响应超时
nb_get_retry 3 #重试次数
delay_before_retry 3 #重试间隔
}
}
real_server 192.168.12.237 80 { #配置服务节点2
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
在配置Keepalived.conf时,需要特别注意配置文件的语法格式,因为Keepalived在启动时并不检测配置文件的正确性,即使没有配置文件,Keepalived也照样能启动起来,因而配置文件一定要正确。
在默认情况下,Keepalived在启动时会查找/etc/Keepalived/Keepalived.conf配置文件,如果你的配置文件放在了其它路径下,可以通过“Keepalived -f”参数指定你所在的配置文件路径即可。
Keepalived.conf配置完毕后,将此文件拷贝到备用Director Server对应的路径下,然后做两个简单的修改即可:
将“state MASTER”更改为“state BACKUP”
将priority 100更改为一个较小的值,这里改为“priority 80”
最后,还要配置集群的Real server节点,以达到与Director Server相互广播通信并忽略arp的目的,脚本的内容已经在前面文章中进行过介绍,这里不做解释。