Use PostgreSQL collect and analyze Operation System statistics

前端之家收集整理的这篇文章主要介绍了Use PostgreSQL collect and analyze Operation System statistics前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当你管理的服务器越来越多的时候,哪个服务器才是你最需要关注的?
通过各个监控维度的排行,可以知道你到底需要关注或者优先关注哪些服务器.
这个通过nagios,cacti等监控软件当然是可以做到的.
不过不太灵活,因为服务器的配置各不一样,关注的维度也不一样,Postgresql数据库的递归调用和窗口函数可以很好的运用来输出各种各样的统计数据,有了数据也就利于展现了。
本文测试环境 :
OS : RHEL5
DB : Postgresql 9.1.2
假设我这里有1000台跑了mongoDB,Postgresql,Oracle,MysqL数据库的服务器需要将它们的SAR报告集中到一台Postgresql数据库中.
集中之后对报告进行分析,找出需要关注的服务器来.
一、首先看看我们要收集什么信息 :
我这里举了一个简单的例子,收集前一天sar的统计报告. 如下 :
1. sar -b
反映系统的每秒读写IO请求等,详情
   
   
@H_403_29@ -b Report I/O and transfer rate statistics. The following values are displayed:
@H_403_29@
@H_403_29@ tps
@H_403_29@ Total number of transfers per second that were issued to physical devices. A transfer is an I/O
@H_403_29@ request to a physical device. Multiple logical requests can be combined into a single I/O request
@H_403_29@ to the device. A transfer is of indeterminate size.
@H_403_29@
@H_403_29@ rtps
@H_403_29@ Total number of read requests per second issued to physical devices.
@H_403_29@
@H_403_29@ wtps
@H_403_29@ Total number of write requests per second issued to physical devices.
@H_403_29@
@H_403_29@ bread/s
@H_403_29@ Total amount of data read from the devices in blocks per second. Blocks are equivalent to sec-
@H_403_29@ tors with 2.4 kernels and newer and therefore have a size of 512 bytes. With older kernels, a
@H_403_29@ block is of indeterminate size.
@H_403_29@
@H_403_29@ bwrtn/s
@H_403_29@ Total amount of data written to devices in blocks per second.
2. sar -B
反映每秒系统写入或从磁盘读出的page数,详情
   
   
@H_403_29@ -B Report paging statistics. The following values are displayed:
@H_403_29@
@H_403_29@ pgpgin/s
@H_403_29@ Total number of kilobytes the system paged in from disk per second. Note: With old kernels
@H_403_29@ (2.2.x) this value is a number of blocks per second (and not kilobytes).
@H_403_29@
@H_403_29@ pgpgout/s
@H_403_29@ Total number of kilobytes the system paged out to disk per second. Note: With old kernels
@H_403_29@ (2.2.x) this value is a number of blocks per second (and not kilobytes).
@H_403_29@
@H_403_29@ fault/s
@H_403_29@ Number of page faults (major + minor) made by the system per second (post 2.5 kernels only).
@H_403_29@ This is not a count of page faults that generate I/O, because some page faults can be resolved
@H_403_29@ without I/O.
@H_403_29@
@H_403_29@ majflt/s
@H_403_29@ Number of major faults the system has made per second, those which have required loading a memory
@H_403_29@ page from disk (post 2.5 kernels only).
3. sar -c
反映系统每秒创建的进程数,如果这个数字很大可能是应用程序连接数据库是短连接,并且请求数据库频繁,而Postgresql采用的是客户端连接过来fork新进程然后这个新进程与客户端进行交互的模式,因此这种情况会造成数据库服务器大量的关闭和创建进程,sar -c能反映这种情况. 使用短连接还有一个坏处就是当系统中使用到sequence,并且这个sequence被大量的这种短连接进程请求,那么它设置的sequence cache没有效果并且会造成大量的跳号.
详情
   
   
@H_403_29@ -c Report process creation activity.
@H_403_29@
@H_403_29@ proc/s
@H_403_29@ Total number of processes created per second.
4. sar -q
反映系统的负载,详情
   
   
@H_403_29@ -q Report queue length and load averages. The following values are displayed:
@H_403_29@
@H_403_29@ runq-sz
@H_403_29@ Run queue length (number of processes waiting for run time).
@H_403_29@
@H_403_29@ plist-sz
@H_403_29@ Number of processes and threads in the process list.
@H_403_29@
@H_403_29@ ldavg-1
@H_403_29@ System load average for the last minute.
@H_403_29@
@H_403_29@ ldavg-5
@H_403_29@ System load average for the past 5 minutes.
@H_403_29@
@H_403_29@ ldavg-15
@H_403_29@ System load average for the past 15 minutes.
5. sar -r
反映系统的内存和SWAP的使用情况,详情
   
   
@H_403_29@ -r Report memory and swap space utilization statistics. The following values are displayed:
@H_403_29@
@H_403_29@ kbmemfree
@H_403_29@ Amount of free memory available in kilobytes.
@H_403_29@
@H_403_29@ kbmemused
@H_403_29@ Amount of used memory in kilobytes. This does not take into account memory used by the kernel
@H_403_29@ itself.
@H_403_29@
@H_403_29@ %memused
@H_403_29@ Percentage of used memory.
@H_403_29@
@H_403_29@ kbbuffers
@H_403_29@ Amount of memory used as buffers by the kernel in kilobytes.
@H_403_29@
@H_403_29@ kbcached
@H_403_29@ Amount of memory used to cache data by the kernel in kilobytes.
@H_403_29@
@H_403_29@ kbswpfree
@H_403_29@ Amount of free swap space in kilobytes.
@H_403_29@
@H_403_29@ kbswpused
@H_403_29@ Amount of used swap space in kilobytes.
@H_403_29@
@H_403_29@ %swpused
@H_403_29@ Percentage of used swap space.
@H_403_29@
@H_403_29@ kbswpcad
@H_403_29@ Amount of cached swap memory in kilobytes. This is memory that once was swapped out, is swapped
@H_403_29@ back in but still also is in the swap area (if memory is needed it doesnt need to be swapped out
@H_403_29@ again because it is already in the swap area. This saves I/O).
6. sar -R
反映每秒被free的内存,新增给buffer的内存,新增给cache的内存. 详情
   
   
@H_403_29@ -R Report memory statistics. The following values are displayed:
@H_403_29@
@H_403_29@ frmpg/s
@H_403_29@ Number of memory pages freed by the system per second. A negative value represents a number of
@H_403_29@ pages allocated by the system. Note that a page has a size of 4 kB or 8 kB according to the
@H_403_29@ machine architecture.
@H_403_29@
@H_403_29@ bufpg/s
@H_403_29@ Number of additional memory pages used as buffers by the system per second. A negative value
@H_403_29@ means fewer pages used as buffers by the system.
@H_403_29@
@H_403_29@ campg/s
@H_403_29@ Number of additional memory pages cached by the system per second. A negative value means fewer
@H_403_29@ pages in the cache.
7. sar -u
反映系统cpu在user,nice,system,iowait,steal,idle的分配比例. 详情
   
   
@H_403_29@ -u Report cpu utilization. The following values are displayed:
@H_403_29@
@H_403_29@ %user
@H_403_29@ Percentage of cpu utilization that occurred while executing at the user level (application).
@H_403_29@
@H_403_29@ %nice
@H_403_29@ Percentage of cpu utilization that occurred while executing at the user level with nice priority.
@H_403_29@
@H_403_29@ %system
@H_403_29@ Percentage of cpu utilization that occurred while executing at the system level (kernel).
@H_403_29@
@H_403_29@ %iowait
@H_403_29@ Percentage of time that the cpu or cpus were idle during which the system had an outstanding disk
@H_403_29@ I/O request.
@H_403_29@
@H_403_29@ %steal
@H_403_29@ Show the percentage of time spent in involuntary wait by the virtual cpu or cpus while the hyper-
@H_403_29@ visor was servicing another virtual processor.
@H_403_29@
@H_403_29@ %idle
@H_403_29@ Percentage of time that the cpu or cpus were idle and the system did not have an outstanding disk
@H_403_29@ I/O request.
@H_403_29@
@H_403_29@ Note: On SMP machines a processor that does not have any activity at all (0.00 for every field) is a
@H_403_29@ disabled (offline) processor.
8. sar -v
inode,file 或其他内核表的报告,详情
   
   
@H_403_29@ -v Report status of inode, file and other kernel tables. The following values are displayed:
@H_403_29@
@H_403_29@ dentunusd
@H_403_29@ Number of unused cache entries in the directory cache.
@H_403_29@
@H_403_29@ file-sz
@H_403_29@ Number of used file handles.
@H_403_29@
@H_403_29@ inode-sz
@H_403_29@ Number of used inode handlers.
@H_403_29@
@H_403_29@ super-sz
@H_403_29@ Number of super block handlers allocated by the kernel.
@H_403_29@
@H_403_29@ %super-sz
@H_403_29@ Percentage of allocated super block handlers with regard to the maximum number of super block
@H_403_29@ handlers that Linux can allocate.
@H_403_29@
@H_403_29@ dquot-sz
@H_403_29@ Number of allocated disk quota entries.
@H_403_29@
@H_403_29@ %dquot-sz
@H_403_29@ Percentage of allocated disk quota entries with regard to the maximum number of cached disk quota
@H_403_29@ entries that can be allocated.
@H_403_29@
@H_403_29@ rtsig-sz
@H_403_29@ Number of queued RT signals.
@H_403_29@
@H_403_29@ %rtsig-sz
@H_403_29@ Percentage of queued RT signals with regard to the maximum number of RT signals that can be
@H_403_29@ queued.
9. sar -w
反映每秒上下文的切换数量,详情
   
   
@H_403_29@ -w Report system switching activity.
@H_403_29@
@H_403_29@ cswch/s
@H_403_29@ Total number of context switches per second.
10. sar -W
反映SWAP每秒被换进或换出的数量,详情
   
   
@H_403_29@ -W Report swapping statistics. The following values are displayed:
@H_403_29@
@H_403_29@ pswpin/s
@H_403_29@ Total number of swap pages the system brought in per second.
@H_403_29@
@H_403_29@ pswpout/s
@H_403_29@ Total number of swap pages the system brought out per second.

二、接下来看看数据库表的设计 :
我这里使用的是sar用户和sar库以及tbs_sar表空间,首先初始化数据库 :
   
   
@H_403_29@create role sar nosuperuser nocreatedb nocreaterole noinherit login encrypted password @H_566_2301@'DIGOAL';
@H_403_29@create tablespace tbs_sar owner digoal location @H_566_2301@'/home/sar/tbs_sar';
@H_403_29@create database sar with owner digoal template template0 encoding @H_566_2301@'UTF8' tablespace tbs_sar;
@H_403_29@grant all on database sar to sar;
@H_403_29@grant all on tablespace tbs_sar to sar;
@H_403_29@\c sar sar
@H_403_29@create schema sar authorization sar;
创建序列,函数 和表 :

@H_403_29@create sequence seq_server_id start with 1 increment by 1;

存放Server信息,本例只为说明方法,所以这个表设计得比较简单,实际使用当中可以加入其他字段,如IDC,维护人,项目名称等。
   
   
@H_403_29@create table server(
@H_403_29@id int primary key,
@H_403_29@ip inet not null unique,
@H_403_29@info text);
根据IP地址获取ServerID的函数,没有则新分配
   
   
@H_403_29@create or replace function get_server_id (i_ip inet) returns int as $BODY$
@H_403_29@declare
@H_403_29@v_id int;
@H_403_29@begin
@H_403_29@select id into v_id from server where ip=i_ip;
@H_403_29@if not found then
@H_403_29@insert into server(id, ip) values(nextval(@H_566_2301@'seq_server_id'::regclass), i_ip);
@H_403_29@select id into v_id from server where ip=i_ip;
@H_403_29@end if;
@H_403_29@return v_id;
@H_403_29@exception
@H_403_29@when others then
@H_403_29@return -1;
@H_403_29@end
@H_403_29@$BODY$ language plpgsql;
根据ServerID获取IP的函数
   
   
@H_403_29@create or replace function get_ip (i_id int) returns inet as $BODY$
@H_403_29@declare
@H_403_29@v_ip inet;
@H_403_29@begin
@H_403_29@select ip into v_ip from server where id=i_id;
@H_403_29@return v_ip;
@H_403_29@exception
@H_403_29@when others then
@H_403_29@return @H_566_2301@'0.0.0.0/0'::inet;
@H_403_29@end
@H_403_29@$BODY$ language plpgsql;
根据ServerID获取服务器info的函数
   
   
create or replace function get_info (i_id int) returns text as $BODY$
declare
v_info text;
begin
select info into v_info from server where id=i_id;
return v_info;
exception
when others then
return @H_566_2301@'no info';
end
$BODY$ language plpgsql;
统计昨天未收集到SAR日志的函数
    
    
@H_403_29@create or replace function get_server_nodata_yesterday() returns setof text as $BODY$
@H_403_29@declare
@H_403_29@v_result text;
@H_403_29@begin
@H_403_29@perform 1 from (select s1.* from server s1 left outer join
@H_403_29@(select * from (select server_id,row_number() over (partition by server_id order by s_date desc) from sar_context where s_date=current_date-1) t1
@H_403_29@where row_number=1) t2 on (s1.id=t2.server_id) where t2.server_id is null) t;
@H_403_29@if found then
@H_403_29@return next @H_566_2301@'sar_context: ';
@H_403_29@return query select s1.ip||@H_566_2301@','||s1.info from server s1 left outer join
@H_403_29@(select * from (select server_id,row_number() over (partition by server_id order by s_date desc) from sar_context where s_date=current_date-1) t1
@H_403_29@where row_number=1) t2 on (s1.id=t2.server_id) where t2.server_id is null;
@H_403_29@end if;
@H_403_29@
@H_403_29@perform 1 from (select s1.* from server s1 left outer join
@H_403_29@(select * from (select server_id,row_number() over (partition by server_id order by s_date desc) from sar_cpu where s_date=current_date-1) t1
@H_403_29@where row_number=1) t2 on (s1.id=t2.server_id) where t2.server_id is null) t;
@H_403_29@if found then
@H_403_29@return next @H_566_2301@'sar_cpu: ';
@H_403_29@return query select s1.ip||@H_566_2301@',row_number() over (partition by server_id order by s_date desc) from sar_cpu where s_date=current_date-1) t1
@H_403_29@where row_number=1) t2 on (s1.id=t2.server_id) where t2.server_id is null;
@H_403_29@end if;
@H_403_29@
@H_403_29@perform 1 from (select s1.* from server s1 left outer join
@H_403_29@(select * from (select server_id,row_number() over (partition by server_id order by s_date desc) from sar_inode where s_date=current_date-1) t1
@H_403_29@where row_number=1) t2 on (s1.id=t2.server_id) where t2.server_id is null) t;
@H_403_29@if found then
@H_403_29@return next @H_566_2301@'sar_inode: ';
@H_403_29@return query select s1.ip||@H_566_2301@',row_number() over (partition by server_id order by s_date desc) from sar_inode where s_date=current_date-1) t1
@H_403_29@where row_number=1) t2 on (s1.id=t2.server_id) where t2.server_id is null;
@H_403_29@end if;
@H_403_29@
@H_403_29@perform 1 from (select s1.* from server s1 left outer join
@H_403_29@(select * from (select server_id,row_number() over (partition by server_id order by s_date desc) from sar_io where s_date=current_date-1) t1
@H_403_29@where row_number=1) t2 on (s1.id=t2.server_id) where t2.server_id is null) t;
@H_403_29@if found then
@H_403_29@return next @H_566_2301@'sar_io: ';
@H_403_29@return query select s1.ip||@H_566_2301@',row_number() over (partition by server_id order by s_date desc) from sar_io where s_date=current_date-1) t1
@H_403_29@where row_number=1) t2 on (s1.id=t2.server_id) where t2.server_id is null;
@H_403_29@end if;
@H_403_29@
@H_403_29@perform 1 from (select s1.* from server s1 left outer join
@H_403_29@(select * from (select server_id,row_number() over (partition by server_id order by s_date desc) from sar_load where s_date=current_date-1) t1
@H_403_29@where row_number=1) t2 on (s1.id=t2.server_id) where t2.server_id is null) t;
@H_403_29@if found then
@H_403_29@return next @H_566_2301@'sar_load: ';
@H_403_29@return query select s1.ip||@H_566_2301@',row_number() over (partition by server_id order by s_date desc) from sar_load where s_date=current_date-1) t1
@H_403_29@where row_number=1) t2 on (s1.id=t2.server_id) where t2.server_id is null;
@H_403_29@end if;
@H_403_29@
@H_403_29@perform 1 from (select s1.* from server s1 left outer join
@H_403_29@(select * from (select server_id,row_number() over (partition by server_id order by s_date desc) from sar_mem where s_date=current_date-1) t1
@H_403_29@where row_number=1) t2 on (s1.id=t2.server_id) where t2.server_id is null) t;
@H_403_29@if found then
@H_403_29@return next @H_566_2301@'sar_mem: ';
@H_403_29@return query select s1.ip||@H_566_2301@',row_number() over (partition by server_id order by s_date desc) from sar_mem where s_date=current_date-1) t1
@H_403_29@where row_number=1) t2 on (s1.id=t2.server_id) where t2.server_id is null;
@H_403_29@end if;
@H_403_29@
@H_403_29@perform 1 from (select s1.* from server s1 left outer join
@H_403_29@(select * from (select server_id,row_number() over (partition by server_id order by s_date desc) from sar_mem_swap where s_date=current_date-1) t1
@H_403_29@where row_number=1) t2 on (s1.id=t2.server_id) where t2.server_id is null) t;
@H_403_29@if found then
@H_403_29@return next @H_566_2301@'sar_mem_swap: ';
@H_403_29@return query select s1.ip||@H_566_2301@',row_number() over (partition by server_id order by s_date desc) from sar_mem_swap where s_date=current_date-1) t1
@H_403_29@where row_number=1) t2 on (s1.id=t2.server_id) where t2.server_id is null;
@H_403_29@end if;
@H_403_29@
@H_403_29@perform 1 from (select s1.* from server s1 left outer join
@H_403_29@(select * from (select server_id,row_number() over (partition by server_id order by s_date desc) from sar_page where s_date=current_date-1) t1
@H_403_29@where row_number=1) t2 on (s1.id=t2.server_id) where t2.server_id is null) t;
@H_403_29@if found then
@H_403_29@return next @H_566_2301@'sar_page: ';
@H_403_29@return query select s1.ip||@H_566_2301@',row_number() over (partition by server_id order by s_date desc) from sar_page where s_date=current_date-1) t1
@H_403_29@where row_number=1) t2 on (s1.id=t2.server_id) where t2.server_id is null;
@H_403_29@end if;
@H_403_29@
@H_403_29@perform 1 from (select s1.* from server s1 left outer join
@H_403_29@(select * from (select server_id,row_number() over (partition by server_id order by s_date desc) from sar_proc where s_date=current_date-1) t1
@H_403_29@where row_number=1) t2 on (s1.id=t2.server_id) where t2.server_id is null) t;
@H_403_29@if found then
@H_403_29@return next @H_566_2301@'sar_proc: ';
@H_403_29@return query select s1.ip||@H_566_2301@',row_number() over (partition by server_id order by s_date desc) from sar_proc where s_date=current_date-1) t1
@H_403_29@where row_number=1) t2 on (s1.id=t2.server_id) where t2.server_id is null;
@H_403_29@end if;
@H_403_29@
@H_403_29@perform 1 from (select s1.* from server s1 left outer join
@H_403_29@(select * from (select server_id,row_number() over (partition by server_id order by s_date desc) from sar_swap where s_date=current_date-1) t1
@H_403_29@where row_number=1) t2 on (s1.id=t2.server_id) where t2.server_id is null) t;
@H_403_29@if found then
@H_403_29@return next @H_566_2301@'sar_swap: ';
@H_403_29@return query select s1.ip||@H_566_2301@',row_number() over (partition by server_id order by s_date desc) from sar_swap where s_date=current_date-1) t1
@H_403_29@where row_number=1) t2 on (s1.id=t2.server_id) where t2.server_id is null;
@H_403_29@end if;
@H_403_29@
@H_403_29@return;
@H_403_29@end
@H_403_29@$BODY$ language plpgsql;
sar信息存放表 :
   
   
@H_403_29@create table sar_io
@H_403_29@(server_id int not null,
@H_403_29@s_date date not null,
@H_403_29@s_time time not null,
@H_403_29@tps numeric,
@H_403_29@rtps numeric,
@H_403_29@wtps numeric,
@H_403_29@bread_p_s numeric,
@H_403_29@bwrtn_p_s numeric,
@H_403_29@unique(server_id,s_date,s_time));
@H_403_29@
@H_403_29@create table sar_page
@H_403_29@(server_id int not null,
@H_403_29@pgpgin_p_s numeric,
@H_403_29@pgpgout_p_s numeric,
@H_403_29@fault_p_s numeric,
@H_403_29@majflt_p_s numeric,s_time));
@H_403_29@
@H_403_29@create table sar_proc
@H_403_29@(server_id int not null,
@H_403_29@proc_p_s numeric,s_time));
@H_403_29@
@H_403_29@create table sar_load
@H_403_29@(server_id int not null,
@H_403_29@runq_sz numeric,
@H_403_29@plist_sz numeric,
@H_403_29@ldavg_1 numeric,
@H_403_29@ldavg_5 numeric,
@H_403_29@ldavg_15 numeric,s_time));
@H_403_29@
@H_403_29@create table sar_mem_swap
@H_403_29@(server_id int not null,
@H_403_29@kbmemfree numeric,
@H_403_29@kbmemused numeric,
@H_403_29@percnt_memused numeric,
@H_403_29@kbbuffers numeric,
@H_403_29@kbcached numeric,
@H_403_29@kbswpfree numeric,
@H_403_29@kbswpused numeric,
@H_403_29@percnt_swpused numeric,
@H_403_29@kbswpcad numeric,s_time));
@H_403_29@
@H_403_29@create table sar_mem
@H_403_29@(server_id int not null,
@H_403_29@frmpg_p_s numeric,
@H_403_29@bufpg_p_s numeric,
@H_403_29@campg_p_s numeric,s_time));
@H_403_29@
@H_403_29@create table sar_cpu
@H_403_29@(server_id int not null,
@H_403_29@percnt_user numeric,
@H_403_29@percnt_nice numeric,
@H_403_29@percnt_system numeric,
@H_403_29@percnt_iowait numeric,
@H_403_29@percnt_steal numeric,
@H_403_29@percnt_idle numeric,s_time));
@H_403_29@
@H_403_29@create table sar_inode
@H_403_29@(server_id int not null,
@H_403_29@dentunusd numeric,
@H_403_29@file_sz numeric,
@H_403_29@inode_sz numeric,
@H_403_29@super_sz numeric,
@H_403_29@percnt_super_sz numeric,
@H_403_29@dquot_sz numeric,
@H_403_29@percnt_dquot_sz numeric,
@H_403_29@rtsig_sz numeric,
@H_403_29@percnt_rtsig_sz numeric,s_time));
@H_403_29@
@H_403_29@create table sar_context
@H_403_29@(server_id int not null,
@H_403_29@cswch_p_s numeric,s_time));
@H_403_29@
@H_403_29@create table sar_swap
@H_403_29@(server_id int not null,
@H_403_29@pswpin_p_s numeric,
@H_403_29@pswpout_p_s numeric,s_time));

三、在需要收集sar报告的操作系统中配置如下程序用来收集sar信息 :
收集用到了Postgresql的psql程序,所以需要在系统中安装Postgresql客户端. 安装过程略.
假设Postgresql数据库的连接信息如下,IP 10.10.10.1,Port 1931,DBNAME sar,USER sar,PASSWORD DIGOAL
配置 ~/.pgpass文件

@H_403_29@10.10.10.1:1931:sar:sar:DIGOAL

@H_403_29@chmod 400 ~/.pgpass


编写sar_collect.sh脚本,用于收集昨天的SAR报告

@H_403_29@vi /home/postgres/sar_collect.sh

   
   
@H_403_29@#!/bin/bash # 环境变量,数据库连接,避免风暴随机等待60秒内 . /home/postgres/.bash_profile DB_URL=@H_566_2301@"-h 10.10.10.1 -p 1931 -U sar -d sar" sleep $(($RANDOM%60)) NET_DEV=@H_566_2301@"`/sbin/route -n|grep UG|awk '{print $8}'|head -n 1`" IP_ADDR=@H_566_2301@"'`/sbin/ip addr show $NET_DEV|grep inet|grep "global $NET_DEV$@H_566_2301@"|awk '{print $2}'`'" SAR_FILE=@H_566_2301@"/var/log/sa/sa`date -d -1day +%d`" SAR_DATE=@H_566_2301@"'`date -d -1day +%Y-%m-%d`'" SERVER_ID=@H_566_2301@"`psql -A -t $DB_URL -c "select * from get_server_id($IP_ADDR)@H_566_2301@"`" # sar -b,sar_io tps rtps wtps bread/s bwrtn/s sql=@H_566_2301@`sar -b -f $SAR_FILE|grep -E 'AM[ ]+([0-9]+|\.+|all|-)|PM[ ]+([0-9]+|\.+|all|-)'|awk '{print "insert into sar_io(server_id,s_date,s_time,tps,rtps,wtps,bread_p_s,bwrtn_p_s) values('$SERVER_ID','$SAR_DATE',","\47"$1$2"\47,$3",$4",$5",$6",$7");"}'` psql $DB_URL -c @H_566_2301@"$sql" # sar -B,sar_page pgpgin/s pgpgout/s fault/s majflt/s sql=@H_566_2301@`sar -B -f $SAR_FILE|grep -E 'AM[ ]+([0-9]+|\.+|all|-)|PM[ ]+([0-9]+|\.+|all|-)'|awk '{print "insert into sar_page(server_id,pgpgin_p_s,pgpgout_p_s,fault_p_s,majflt_p_s) values('$SERVER_ID',$6");"}'` psql $DB_URL -c @H_566_2301@"$sql" # sar -c,sar_proc proc/s sql=@H_566_2301@`sar -c -f $SAR_FILE|grep -E 'AM[ ]+([0-9]+|\.+|all|-)|PM[ ]+([0-9]+|\.+|all|-)'|awk '{print "insert into sar_proc(server_id,proc_p_s) values('$SERVER_ID',$3");"}'` psql $DB_URL -c @H_566_2301@"$sql" # sar -q,sar_load runq-sz plist-sz ldavg-1 ldavg-5 ldavg-15 sql=@H_566_2301@`sar -q -f $SAR_FILE|grep -E 'AM[ ]+([0-9]+|\.+|all|-)|PM[ ]+([0-9]+|\.+|all|-)'|awk '{print "insert into sar_load(server_id,runq_sz,plist_sz,ldavg_1,ldavg_5,ldavg_15) values('$SERVER_ID',$7");"}'` psql $DB_URL -c @H_566_2301@"$sql" # sar -r,sar_mem_swap kbmemfree kbmemused %memused kbbuffers kbcached kbswpfree kbswpused %swpused kbswpcad sql=@H_566_2301@`sar -r -f $SAR_FILE|grep -E 'AM[ ]+([0-9]+|\.+|all|-)|PM[ ]+([0-9]+|\.+|all|-)'|awk '{print "insert into sar_mem_swap(server_id,kbmemfree,kbmemused,percnt_memused,kbbuffers,kbcached,kbswpfree,kbswpused,percnt_swpused,kbswpcad) values('$SERVER_ID',$7",$8",$9",$10",$11");"}'` psql $DB_URL -c @H_566_2301@"$sql" # sar -R,sar_mem frmpg/s bufpg/s campg/s sql=@H_566_2301@`sar -R -f $SAR_FILE|grep -E 'AM[ ]+([0-9]+|\.+|all|-)|PM[ ]+([0-9]+|\.+|all|-)'|awk '{print "insert into sar_mem(server_id,frmpg_p_s,bufpg_p_s,campg_p_s) values('$SERVER_ID',$5");"}'` psql $DB_URL -c @H_566_2301@"$sql" # sar -u,sar_cpu %user %nice %system %iowait %steal %idle sql=@H_566_2301@`sar -u -f $SAR_FILE|grep -E 'AM[ ]+([0-9]+|\.+|all|-)|PM[ ]+([0-9]+|\.+|all|-)'|awk '{print "insert into sar_cpu(server_id,percnt_user,percnt_nice,percnt_system,percnt_iowait,percnt_steal,percnt_idle) values('$SERVER_ID',$9");"}'` psql $DB_URL -c @H_566_2301@"$sql" # sar -v,sar_inode dentunusd file-sz inode-sz super-sz %super-sz dquot-sz %dquot-sz rtsig-sz %rtsig-sz sql=@H_566_2301@`sar -v -f $SAR_FILE|grep -E 'AM[ ]+([0-9]+|\.+|all|-)|PM[ ]+([0-9]+|\.+|all|-)'|awk '{print "insert into sar_inode(server_id,dentunusd,file_sz,inode_sz,super_sz,percnt_super_sz,dquot_sz,percnt_dquot_sz,rtsig_sz,percnt_rtsig_sz) values('$SERVER_ID',$11");"}'` psql $DB_URL -c @H_566_2301@"$sql" # sar -w,sar_context cswch/s sql=@H_566_2301@`sar -w -f $SAR_FILE|grep -E 'AM[ ]+([0-9]+|\.+|all|-)|PM[ ]+([0-9]+|\.+|all|-)'|awk '{print "insert into sar_context(server_id,cswch_p_s) values('$SERVER_ID',$3");"}'` psql $DB_URL -c @H_566_2301@"$sql" # sar -W,sar_swap pswpin/s pswpout/s sql=@H_566_2301@`sar -W -f $SAR_FILE|grep -E 'AM[ ]+([0-9]+|\.+|all|-)|PM[ ]+([0-9]+|\.+|all|-)'|awk '{print "insert into sar_swap(server_id,pswpin_p_s,pswpout_p_s) values('$SERVER_ID',$4");"}'` psql $DB_URL -c @H_566_2301@"$sql"
@H_403_29@
@H_403_29@# Author : Digoal.Zhou
@H_403_29@# THE END

修改权限 :

chmod 500 sar_collect.sh

新建执行计划,

@H_403_29@crontab -e

@H_403_29@1 2 * * * @H_566_2301@/home/postgres/sar_collect.sh


四、然后看看几条简单的报告sql,使用row_number窗口函数 :
   
   
@H_403_29@# 昨天15分钟最大负载排名前10,后一条为平均值排行 # 负载过高需要关注这些服务器上运行的数据库和业务是否健康,例如是否需要建索引,是否需要使用绑定变量等. select get_ip(server_id),* from (select *,row_number() over (partition by server_id order by ldavg_15 desc) from sar_load where s_date=current_date-1) t where row_number=1 order by ldavg_15 desc limit 10; select get_info(server_id),get_ip(server_id),round(avg(ldavg_15),2) ldavg_15 from sar_load where s_date=current_date-1 group by server_id,s_date order by ldavg_15 desc limit 10; # 昨天最大读请求数排名前10,后一条为平均值排行 # 读请求过高需要关注这些服务器上运行的数据库和业务是否健康,是否需要加内存,是否需要对存储性能扩容等. select get_ip(server_id),row_number() over (partition by server_id order by rtps desc) from sar_io where s_date=current_date-1) t where row_number=1 order by rtps desc limit 10; select get_info(server_id),round(avg(rtps),2) rtps from sar_io where s_date=current_date-1 group by server_id,s_date order by rtps desc limit 10; # 昨天最大写请求数排名前10,后一条为平均值排行 # 写请求过高需要关注这些服务器上运行的数据库和业务是否健康,例如是否需要减少索引,是否需要使用异步IO,是否需要对存储性能进行扩容等. select get_ip(server_id),row_number() over (partition by server_id order by wtps desc) from sar_io where s_date=current_date-1) t where row_number=1 order by wtps desc limit 10; select get_info(server_id),round(avg(wtps),2) wtps from sar_io where s_date=current_date-1 group by server_id,s_date order by wtps desc limit 10; # 昨天最大iowait排名前10,后一条为平均值排行 # iowait过高需要关注这些服务器上运行的数据库和业务是否健康,例如是否需要加内存,是否需要将常用数据放入内存,是否需要对存储性能进行扩容等. select get_ip(server_id),row_number() over (partition by server_id order by percnt_iowait desc) from sar_cpu where s_date=current_date-1) t where row_number=1 order by percnt_iowait desc limit 10; select get_info(server_id),round(avg(percnt_iowait),2) percnt_iowait from sar_cpu where s_date=current_date-1 group by server_id,s_date order by percnt_iowait desc limit 10; # 昨天最大swap页进出排名前10,后一条为平均值排行 # swap也进出过高需要关注这些服务器上运行的数据库和业务是否健康,是否需要将常用数据放入内存等. select get_ip(server_id),row_number() over (partition by server_id order by pswpin_p_s+pswpout_p_s desc) from sar_swap where s_date=current_date-1) t where row_number=1 order by pswpin_p_s+pswpout_p_s desc limit 10; select get_info(server_id),round(avg(pswpin_p_s+pswpout_p_s),2) pswpin_out_p_s from sar_swap where s_date=current_date-1 group by server_id,s_date order by pswpin_out_p_s desc limit 10; # 昨天最大SWAP使用比例前10,后一条为平均值排行 # SWAP使用率过高需要关注这些服务器上运行的数据库和业务是否健康,是否需要调整数据库参数,是否需要使用大页等. select get_ip(server_id),row_number() over (partition by server_id order by percnt_swpused desc) from sar_mem_swap where s_date=current_date-1) t where row_number=1 order by percnt_swpused desc limit 10; select get_info(server_id),round(avg(percnt_swpused),2) percnt_swpused from sar_mem_swap where s_date=current_date-1 group by server_id,s_date order by percnt_swpused desc limit 10; # 昨天每秒新建进程排名前10,后一条为平均值排行 # 每秒新建进程数过高需要关注这些服务器上运行的数据库和业务是否健康,例如是否需要加个数据库连接池使用长连接,Oracle是否需要使用共享连接,应用程序是否可以将短连接改成长连接的模式等. select get_ip(server_id),row_number() over (partition by server_id order by proc_p_s desc) from sar_proc where s_date=current_date-1) t where row_number=1 order by proc_p_s desc limit 10; select get_info(server_id),round(avg(proc_p_s),2) proc_p_s from sar_proc where s_date=current_date-1 group by server_id,s_date order by proc_p_s desc limit 10;
报告如图 :


五、最后通过邮件将报告发送给自己 :
发送邮件脚本 :
  
  
@H_403_29@#!/bin/bash
@H_403_29@. /home/postgres/.bash_profile
@H_403_29@
@H_403_29@EMAIL=@H_566_2301@"digoal@126.com"
@H_403_29@
@H_403_29@echo -e @H_566_2301@`date +%F\ %T` >@H_566_2301@/tmp/sar_report.log echo -e @H_566_2301@"\n---- WeeklyAvgValue TOP10: ----\n" >>@H_566_2301@/tmp/sar_report.log echo -e @H_566_2301@"\n1. ldavg_15 TOP10 :\n" >>@H_566_2301@/tmp/sar_report.log psql -h 127.0.0.1 sar sar -c @H_566_2301@"select get_info(server_id),get_ip(server_id),round(avg(ldavg_15),2) ldavg_15 from sar_load where s_date<=current_date-1 and s_date>=current_date-7 group by server_id order by ldavg_15 desc limit 10;" >>@H_566_2301@/tmp/sar_report.log echo -e @H_566_2301@"\n2. rtps TOP10 :\n" >>@H_566_2301@/tmp/sar_report.log psql -h 127.0.0.1 sar sar -c @H_566_2301@"select get_info(server_id),round(avg(rtps),2) rtps from sar_io where s_date<=current_date-1 and s_date>=current_date-7 group by server_id order by rtps desc limit 10;" >>@H_566_2301@/tmp/sar_report.log echo -e @H_566_2301@"\n3. wtps TOP10 :\n" >>@H_566_2301@/tmp/sar_report.log psql -h 127.0.0.1 sar sar -c @H_566_2301@"select get_info(server_id),round(avg(wtps),2) wtps from sar_io where s_date<=current_date-1 and s_date>=current_date-7 group by server_id order by wtps desc limit 10;" >>@H_566_2301@/tmp/sar_report.log echo -e @H_566_2301@"\n4. iowait TOP10 :\n" >>@H_566_2301@/tmp/sar_report.log psql -h 127.0.0.1 sar sar -c @H_566_2301@"select get_info(server_id),round(avg(percnt_iowait),2) percnt_iowait from sar_cpu where s_date<=current_date-1 and s_date>=current_date-7 group by server_id order by percnt_iowait desc limit 10;" >>@H_566_2301@/tmp/sar_report.log echo -e @H_566_2301@"\n5. swap_page_in_out TOP10 :\n" >>@H_566_2301@/tmp/sar_report.log psql -h 127.0.0.1 sar sar -c @H_566_2301@"select get_info(server_id),round(avg(pswpin_p_s+pswpout_p_s),2) pswpin_out_p_s from sar_swap where s_date<=current_date-1 and s_date>=current_date-7 group by server_id order by pswpin_out_p_s desc limit 10;" >>@H_566_2301@/tmp/sar_report.log echo -e @H_566_2301@"\n6. swap_usage TOP10 :\n" >>@H_566_2301@/tmp/sar_report.log psql -h 127.0.0.1 sar sar -c @H_566_2301@"select get_info(server_id),round(avg(percnt_swpused),2) percnt_swpused from sar_mem_swap where s_date<=current_date-1 and s_date>=current_date-7 group by server_id order by percnt_swpused desc limit 10;" >>@H_566_2301@/tmp/sar_report.log echo -e @H_566_2301@"\n7. newproc_p_s TOP10 :\n" >>@H_566_2301@/tmp/sar_report.log psql -h 127.0.0.1 sar sar -c @H_566_2301@"select get_info(server_id),round(avg(proc_p_s),2) proc_p_s from sar_proc where s_date<=current_date-1 and s_date>=current_date-7 group by server_id order by proc_p_s desc limit 10;" >>@H_566_2301@/tmp/sar_report.log echo -e @H_566_2301@"\n---- DailyAvgValue TOP10: ----\n" >>@H_566_2301@/tmp/sar_report.log echo -e @H_566_2301@"\n1. ldavg_15 TOP10 :\n" >>@H_566_2301@/tmp/sar_report.log psql -h 127.0.0.1 sar sar -c @H_566_2301@"select get_info(server_id),2) ldavg_15 from sar_load where s_date=current_date-1 group by server_id order by ldavg_15 desc limit 10;" >>@H_566_2301@/tmp/sar_report.log echo -e @H_566_2301@"\n2. rtps TOP10 :\n" >>@H_566_2301@/tmp/sar_report.log psql -h 127.0.0.1 sar sar -c @H_566_2301@"select get_info(server_id),2) rtps from sar_io where s_date=current_date-1 group by server_id order by rtps desc limit 10;" >>@H_566_2301@/tmp/sar_report.log echo -e @H_566_2301@"\n3. wtps TOP10 :\n" >>@H_566_2301@/tmp/sar_report.log psql -h 127.0.0.1 sar sar -c @H_566_2301@"select get_info(server_id),2) wtps from sar_io where s_date=current_date-1 group by server_id order by wtps desc limit 10;" >>@H_566_2301@/tmp/sar_report.log echo -e @H_566_2301@"\n4. iowait TOP10 :\n" >>@H_566_2301@/tmp/sar_report.log psql -h 127.0.0.1 sar sar -c @H_566_2301@"select get_info(server_id),2) percnt_iowait from sar_cpu where s_date=current_date-1 group by server_id order by percnt_iowait desc limit 10;" >>@H_566_2301@/tmp/sar_report.log echo -e @H_566_2301@"\n5. swap_page_in_out TOP10 :\n" >>@H_566_2301@/tmp/sar_report.log psql -h 127.0.0.1 sar sar -c @H_566_2301@"select get_info(server_id),2) pswpin_out_p_s from sar_swap where s_date=current_date-1 group by server_id order by pswpin_out_p_s desc limit 10;" >>@H_566_2301@/tmp/sar_report.log echo -e @H_566_2301@"\n6. swap_usage TOP10 :\n" >>@H_566_2301@/tmp/sar_report.log psql -h 127.0.0.1 sar sar -c @H_566_2301@"select get_info(server_id),2) percnt_swpused from sar_mem_swap where s_date=current_date-1 group by server_id order by percnt_swpused desc limit 10;" >>@H_566_2301@/tmp/sar_report.log echo -e @H_566_2301@"\n7. newproc_p_s TOP10 :\n" >>@H_566_2301@/tmp/sar_report.log psql -h 127.0.0.1 sar sar -c @H_566_2301@"select get_info(server_id),2) proc_p_s from sar_proc where s_date=current_date-1 group by server_id order by proc_p_s desc limit 10;" >>@H_566_2301@/tmp/sar_report.log echo -e @H_566_2301@"\n---- DailyMaxValue TOP10: ----\n" >>@H_566_2301@/tmp/sar_report.log echo -e @H_566_2301@"\n1. ldavg_15 TOP10 :\n" >>@H_566_2301@/tmp/sar_report.log psql -h 127.0.0.1 sar sar -c @H_566_2301@"select get_info(server_id),ldavg_15 from (select *,row_number() over (partition by server_id order by ldavg_15 desc) from sar_load where s_date=current_date-1) t where row_number=1 order by ldavg_15 desc limit 10;" >>@H_566_2301@/tmp/sar_report.log echo -e @H_566_2301@"\n2. rtps TOP10 :\n" >>@H_566_2301@/tmp/sar_report.log psql -h 127.0.0.1 sar sar -c @H_566_2301@"select get_info(server_id),bwrtn_p_s from (select *,row_number() over (partition by server_id order by rtps desc) from sar_io where s_date=current_date-1) t where row_number=1 order by rtps desc limit 10;" >>@H_566_2301@/tmp/sar_report.log echo -e @H_566_2301@"\n3. wtps TOP10 :\n" >>@H_566_2301@/tmp/sar_report.log psql -h 127.0.0.1 sar sar -c @H_566_2301@"select get_info(server_id),row_number() over (partition by server_id order by wtps desc) from sar_io where s_date=current_date-1) t where row_number=1 order by wtps desc limit 10;" >>@H_566_2301@/tmp/sar_report.log echo -e @H_566_2301@"\n4. iowait TOP10 :\n" >>@H_566_2301@/tmp/sar_report.log psql -h 127.0.0.1 sar sar -c @H_566_2301@"select get_info(server_id),percnt_idle from (select *,row_number() over (partition by server_id order by percnt_iowait desc) from sar_cpu where s_date=current_date-1) t where row_number=1 order by percnt_iowait desc limit 10;" >>@H_566_2301@/tmp/sar_report.log echo -e @H_566_2301@"\n5. swap_page_in_out TOP10 :\n" >>@H_566_2301@/tmp/sar_report.log psql -h 127.0.0.1 sar sar -c @H_566_2301@"select get_info(server_id),pswpout_p_s from (select *,row_number() over (partition by server_id order by pswpin_p_s+pswpout_p_s desc) from sar_swap where s_date=current_date-1) t where row_number=1 order by pswpin_p_s+pswpout_p_s desc limit 10;" >>@H_566_2301@/tmp/sar_report.log echo -e @H_566_2301@"\n6. swap_usage TOP10 :\n" >>@H_566_2301@/tmp/sar_report.log psql -h 127.0.0.1 sar sar -c @H_566_2301@"select get_info(server_id),kbswpcad from (select *,row_number() over (partition by server_id order by percnt_swpused desc) from sar_mem_swap where s_date=current_date-1) t where row_number=1 order by percnt_swpused desc limit 10;" >>@H_566_2301@/tmp/sar_report.log echo -e @H_566_2301@"\n7. newproc_p_s TOP10 :\n" >>@H_566_2301@/tmp/sar_report.log psql -h 127.0.0.1 sar sar -c @H_566_2301@"select get_info(server_id),proc_p_s from (select *,row_number() over (partition by server_id order by proc_p_s desc) from sar_proc where s_date=current_date-1) t where row_number=1 order by proc_p_s desc limit 10;" >>@H_566_2301@/tmp/sar_report.log echo -e @H_566_2301@"\n---- get_server_nodata_yesterday: ----\n" >>@H_566_2301@/tmp/sar_report.log psql -h 127.0.0.1 sar sar -c @H_566_2301@"select * from get_server_nodata_yesterday();" >>@H_566_2301@/tmp/sar_report.log
@H_403_29@
@H_403_29@cat /tmp/sar_report.log|mutt -s @H_566_2301@"`date +$F` DB Servers RS Consume Top10" $EMAIL
@H_403_29@# Author : Digoal.Zhou
@H_403_29@# THE END
配置mutt环境,假设数据库编码为UTF-8,否则中文可能出错.
  
  
@H_403_29@vi ~/.muttrc
@H_403_29@set envelope_from=yes
@H_403_29@set from=digoal@126.com
@H_403_29@set realname=@H_566_2301@"德哥"
@H_403_29@set use_from=yes
@H_403_29@set charset=@H_566_2301@"UTF-8"
六、其他,
1. 展现可以通过WEB形式来做,这里只是举了个简单的收集和统计的例子,未设计WEB开发.
2. 1000台服务器一天收集的这些sar日志数据量约200MB. 按照现在的硬盘容量,放几年没问题.
【参考】
man sar

猜你在找的Postgre SQL相关文章