2.linux monitor cpu&mem收集資料

tolilong發表於2016-02-27
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-2006113/,如需轉載,請註明出處,否則將追究法律責任。

相關文章