[Shell] monitor cpu&mem收集数据

oracle6机器是收集cpu&mem的机器,访问其他的linux服务器


1.在/etc/hosts添加IP和hostname对应关系
[root@oracle6 ~]# more /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.99.35 oracle6
172.16.99.19 oracle
172.16.99.20 xxxxx
192.168.xxx.xxx standby
192.168.xxx.xxx xxxxxxx1
192.168.xxx.xxx xxxxxxx2


2.在所有对应的机器建立相应的user
[root@oracle6 ~]# useradd mon
[root@oracle6 ~]# passwd mon
Changing password for user mon.
New password: 
BAD PASSWORD: it is WAY too short
BAD PASSWORD: is too simple
Retype new password: 
passwd: all authentication tokens updated successfully.


3.配置ssh
[root@oracle6 .ssh]# su - mon
[mon@oracle6 ~]$ ll .ssh
ls: cannot access .ssh: No such file or directory
[mon@oracle6 ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/mon/.ssh/id_rsa): 
Created directory '/home/mon/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/mon/.ssh/id_rsa.
Your public key has been saved in /home/mon/.ssh/id_rsa.pub.
The key fingerprint is:
5c:0e:06:09:64:aa:ae:47:1d:9e:11:e9:58:d8:9c:36 mon@oracle6
The key's randomart image is:
+--[ RSA 2048]----+
|   =++..         |
|  .oE ..         |
|  .= o  o .      |
| .. +  o +       |
|.  o +  S .      |
|. . +            |
| o               |
|. .              |
|..               |
+-----------------+
[mon@oracle6 ~]$ ll .ssh
total 12
-rw-------. 1 mon mon 1675 Feb 21 17:16 id_rsa
-rw-r--r--. 1 mon mon  393 Feb 21 17:16 id_rsa.pub
-rw-r--r--. 1 mon mon  401 Feb 21 17:17 known_hosts


[mon@oracle6 ~]$ ssh mon@oracle "cat >> ~/.ssh/authorized_keys" < ~/.ssh/id_rsa.pub
The authenticity of host 'oracle (172.16.99.19)' can't be established.
RSA key fingerprint is b6:9f:be:8a:ad:1e:e8:c8:1f:b4:75:04:01:69:0e:5e.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'oracle,172.16.99.19' (RSA) to the list of known hosts.
mon@oracle's password: mon
[mon@oracle6 ~]$ ssh mon@oracle
Last login: Sun Feb 21 17:18:33 2016 from s-file-backend.domain.com


其中遇到过问题就是ssh免密码登录配置了,但是ssh mon@topaz还是需要输入密码.
设置topaz:/home/mon/.ssh/authorized_keys的属性为600(原来为664)之后,就可以了。


authorized_keys的正确属性为600
.ssh的正确属性为700




4.创建收集数据的tablespace,schema,table
SQL> create tablespace mon datafile '/home/oracle/data/mon.dbf' size 8G autoextend off;


Tablespace created.


SQL> create user mon identified by mon default tablespace mon;


User created.


SQL> grant connect,resource to mon;


Grant succeeded.


SQL> conn mon/mon
Connected.


SQL> create table mon_cpumem(
  2  hostname varchar2(20),
  3  datetime varchar2(30),
  4  processrun int,
  5  mem_used_pct number(10,2),
  6  swapin int,  
  7  swapout int,
  8  cpu_user int,
  9  cpu_sys int,
 10  cpu_idle int,
 11  cpu_wait int,
 12  cpu_stole int);




5.收集数据的shell script
[mon@oracle6 source]$ more mon_cpumem
#!/bin/bash
export ORACLE_BASE=/u01/oracle
export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/dbhome_1
export ORACLE_SID=wlcspdb


HOSTNAME=/home/mon/source/mon_cpumem_hostname
MEM_TOTAL=0
MEM_FREE=0
DATETIME=`date +"%Y/%m/%d %H:%M:%S"`
echo $DATETIME


cat $HOSTNAME | while
   read LINE
do
   MEM_TOTAL=`echo "cat /proc/meminfo | grep -i memtotal" | ssh -Tq mon@$LINE`
   VMSTATS=`echo "vmstat 1 3 | tail -1" | ssh -Tq mon@$LINE`
   
   MEM_TOTAL=`echo $MEM_TOTAL | awk '{print $2}'`
   MEM_FREE=`echo $VMSTATS | awk '{print $4}'`
   let MEM_FREE_PCT=$MEM_FREE*100/$MEM_TOTAL
   let MEM_USED_PCT=100-$MEM_FREE_PCT
 
   PROCESS_RUN=`echo $VMSTATS | awk '{print $1}'`
   SWAP_IN=`echo $VMSTATS | awk '{print $7}'`
   SWAP_OUT=`echo $VMSTATS | awk '{print $8}'`
   CPU_USER=`echo $VMSTATS | awk '{print $13}'`
   CPU_SYS=`echo $VMSTATS | awk '{print $14}'`
   CPU_IDLE=`echo $VMSTATS | awk '{print $15}'`
   CPU_WAIT=`echo $VMSTATS | awk '{print $16}'`
   CPU_STOLE=`echo $VMSTATS | awk '{print $17}'`
 
   $ORACLE_HOME/bin/sqlplus -s mon/mon << EOF >> /home/mon/log/mon_cpumem.log
   insert into mon_cpumem values   ('$LINE','$DATETIME','$PROCESS_RUN','$MEM_USED_PCT','$SWAP_IN','$SWAP_OUT','$CPU_USER','$CPU_SYS','$CPU_IDLE','$CPU_WAIT','$CPU_STOLE');
   commit;
   exit;
EOF
  
done


6.定时任务crontab
[mon@oracle6 source]$ crontab -l
* * * * * /home/mon/source/mon_cpumem > /dev/null 2>&1

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/24237320/viewspace-2006112/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/24237320/viewspace-2006112/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值