- 论坛徽章:
- 0
|
#include "stdio.h"
#include "stdlib.h"
#include "errno.h"
#include "string.h"
#include "netdb.h"
#include "sys/types.h"
#include "netinet/in.h"
#include "sys/socket.h"
#define SVRPORT 5656
main(){
int sock_fd,rcvnum;
char sSBuf[150],sRBuf[150];
FILE *fp;
struct sockaddr_in serv_addr;
while (1){
memset(&sSBuf,0,sizeof(sSBuf));
memset(&sRBuf,0,sizeof(sRBuf));
/* ============================
= 获得系统状态 =
============================ */
if((fp = popen("/bin/sgetsys","r") == NULL){
printf("popen error !!!!!\n";
pclose(fp);
exit(1);
}
if((fgets(sSBuf,150,fp)) == NULL){
printf("sgetsys fail !!!!!\n";
exit(1);
}
if ( pclose(fp) == -1){
printf("pclose error !!!!!\n";
exit(1);
}
/* ============================
= socket 传送数据 =
============================ */
if((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
perror("create socket error !!!!!";
exit(1);
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(SVRPORT);
serv_addr.sin_addr.s_addr = inet_addr("10.1.2.17";
bzero(&(serv_addr.sin_zero),;
if( connect(sock_fd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) == -1){
perror("connect error !!!!!";
exit(1);
}
if( send(sock_fd, sSBuf, strlen(sSBuf), 0) == -1){
perror("send error !!!!!";
exit(1);
}
close(sock_fd);
}
}
####################################################################
其中 sgetsys 为
sgetsys
#生成文件格式为:
# cpu1空闲|cpu2空闲|用户数|%busy|%usr|%sys|%wio|%idle
#echo `date '+%H:%M:%S'`
stat=`sar 1 1|tail -1|awk '{print $2"|"$3"|"$4"|"$5}'`
#stat=`sar -d 1 1|tail -1|awk '{print $3}'`"|"$stat
stat=`who | wc -l | sed "s/ //g"`"|"$stat
stat=`ps -ef -o comm -o pcpu|sort -k 2|grep CPU2|awk '{print $3}'`"|"$stat
stat=`ps -ef -o comm -o pcpu|sort -k 2|grep CPU1|awk '{print $3}'`"|"$stat
echo $stat
#echo `date '+%H:%M:%S'` |
|