⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cps.c

📁 linux/unix下c/s形式的资源监视,客户端负责搜集机器,再传送到服务端.
💻 C
字号:
/* * List a host's process list, like 'ps'. * But, this program can get remote machine's process list. * * Copyright (c) 2004, by:      Jian Shen *    All rights reserved.      Peking University, China *                             <shenjian@net.pku.edu.cn> * * This file may be used subject to the terms and conditions of the * GNU Library General Public License Version 2, or any later version * at your option, as published by the Free Software Foundation. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU Library General Public License for more details. * */#include "statinfo.h"#include <unistd.h>#include <stdlib.h>#include <fcntl.h>#include <sys/types.h>#include <sys/stat.h>#include <arpa/inet.h>#include <netdb.h>#include <string.h>#include <stdio.h>#include <getopt.h>char* get_host_ip(const char* hostname);void parse_line(char* line, proc_stat_t* proc);void print_proc_list(proc_list_t* plist);void print_proc_list_filter_user(proc_list_t* plist, const char* user);void usage(){	printf("usage: cps [-u user] hostname\n");}void help(){	printf("\nPrint process list of a host.\n");	printf("usage: cps [-u user] hostname\n");	printf("\nreport bugs to <shenjian@net.pku.edu.cn>.\n");	printf("\n");}int main(int argc, char* argv[]){	proc_list_t *all_procs;	char* target;	char filename[100];	FILE* f;	int rv;	int count;	char* line;	size_t len = 0;	/* deal with options */	int USER_FILTER = 0;	int next_opt;	char** program = argv;	int argcnt = argc;	char username[15];	const char* short_options = "hu:";	const struct option long_options[] = {		{"help", 0, NULL, 'h'},		{"user", 1, NULL, 'u'}	};	do{		next_opt = getopt_long(argc, argv, short_options, long_options, NULL);		argcnt -= 1;		switch(next_opt) {		case 'h' :				/* -h or --help */			help();			exit(1);		case '?' :				/* invalid option */			usage();			exit(1);		case 'u':				/* -u or --user */			USER_FILTER = 1;			strcpy(username, optarg);			program += 2;			argcnt -= 1;			break;		case ':' :				/* missing parameters */			usage();			exit(1);		case -1 :				/* end of option list */			break;		default:			printf("Something wrong with getopt_long.\n");			exit(1);		}	}while(next_opt != -1);	program += 1;	if(argcnt != 1) {		usage();		exit(1);	}	target = get_host_ip(program[0]);	if(target == NULL) {		fprintf(stderr, "Who is \"%s\"?\n", program[0]);		exit(1);	}	sprintf(filename, "/proc/cluster/%s/ps", target);	if((f=fopen(filename, "r")) == NULL) {		fprintf(stderr, "Failed to open: %s\n", filename);		exit(1);	}//	printf("file name: %s\n", filename);	all_procs = (proc_list_t*)malloc(sizeof(proc_list_t));	if(all_procs == NULL) {		fprintf(stderr, "Out of memory!\n");		fclose(f);		exit(1);	}	count = 0;	while(1) {		rv = getline(&line, &len, f);		if(rv == -1)			break;		parse_line(line, &all_procs->procs[count]);				count ++;	}	fclose(f);			all_procs->num_of_procs = count;	if(USER_FILTER)		print_proc_list_filter_user(all_procs, username);	else		print_proc_list(all_procs);	free(all_procs);	return 0;}/* get host's ip address by its alias according to /etc/hosts */char* get_host_ip(const char* hostname){	char* hostIP;	struct hostent * h;	struct in_addr* in;	hostIP = (char*)malloc(20*sizeof(char));	/* get host structure by host name */	h = gethostbyname(hostname);	if(h != NULL) {		in = (struct in_addr*)h->h_addr;		sprintf(hostIP,"%s", inet_ntoa(*in));		return hostIP;			/* caller free */	}else {//		fprintf(stderr, "gethostbyname failed.\n");		return NULL;	}}void print_proc_list(proc_list_t* plist){	int i;	printf("%-10s %5s %5s %5s %5s %8s%8s %4s  %s\n",		   "USER", "PID", "PPID", "%CPU", "%MEM", "VSZ", "RSS", 		   "STAT", "COMMAND");	for(i=0; i<plist->num_of_procs; i++) {		printf("%-10s %5d %5d %5s %5s %8d%8d %4c  %s\n", 			   plist->procs[i].user,			   plist->procs[i].pid,			   plist->procs[i].ppid, 			   plist->procs[i].cpu_percent,			   plist->procs[i].mem_percent,			   plist->procs[i].vsize,			   plist->procs[i].rss,			   plist->procs[i].state, 			   plist->procs[i].command			);	}	printf("\nNumber of Processes: %d\n", plist->num_of_procs);}void print_proc_list_filter_user(proc_list_t* plist, const char* user){	int i;	printf("%-10s %5s %5s %5s %5s %8s%8s %4s  %s\n",		   "USER", "PID", "PPID", "%CPU", "%MEM", "VSZ", "RSS", 		   "STAT", "COMMAND");	for(i=0; i<plist->num_of_procs; i++) {		if(strcmp(user, plist->procs[i].user) == 0)			printf("%-10s %5d %5d %5s %5s %8d%8d %4c  %s\n", 				   plist->procs[i].user,				   plist->procs[i].pid,				   plist->procs[i].ppid, 				   plist->procs[i].cpu_percent,				   plist->procs[i].mem_percent,				   plist->procs[i].vsize,				   plist->procs[i].rss,				   plist->procs[i].state, 				   plist->procs[i].command				);	}}void parse_line(char* line, proc_stat_t* proc){	char *token;	char str[300];	const char delim[] = "\t\n";	strcpy(str, line);	sprintf(proc->user, "%s", strtok(str, delim));	proc->pid = atoi(strtok(NULL, delim));	proc->ppid = atoi(strtok(NULL, delim));	sprintf(proc->cpu_percent, "%s", strtok(NULL, delim));	sprintf(proc->mem_percent, "%s", strtok(NULL, delim));	proc->vsize = atoi(strtok(NULL, delim));	proc->rss = atoi(strtok(NULL, delim));	token = strtok(NULL, delim);	proc->state = (char)*token;	proc->last_cpu = atoi(strtok(NULL, delim));	proc->page_fault = atoi(strtok(NULL, delim));	sprintf(proc->usr_time, "%s", strtok(NULL, delim));	sprintf(proc->sys_time, "%s", strtok(NULL, delim));	token =  strtok(NULL, delim);	proc->time_lasting.day = 0;	proc->time_lasting.hour = 0;	proc->time_lasting.minute = 0;	proc->time_lasting.second = 0;	token = strtok(NULL, delim);	if(token == NULL) {		sprintf(proc->command, "N/A");			return;	}else {		sprintf(proc->command, "%s", token);	}	token = strtok(NULL, delim);	if(token == NULL) {		sprintf(proc->cwd, "N/A");		return;	}else {		sprintf(proc->cwd, "%s", token);	}	token = strtok(NULL, delim);	if(token == NULL) {		sprintf(proc->exe, "N/A");		}else {		sprintf(proc->exe, "%s", token);	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -