📄 monitor.c
字号:
#include <sys/types.h>
#include <sys/stat.h>
#include<sys/file.h>
#include<dirent.h>
#include<fcntl.h>
#include<math.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include<time.h>
#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
#include<stdlib.h>
static GtkWidget *clocker;/*存放时间*/
static GdkPixmap *pixmap = NULL;/*存放CPU使用曲线图*/
GdkGC *my_gc_red;
GdkColor color;
long user2=0,system2=0,nice2=0,idle2=0,iowait2=0,irq2=0,softirq2=0; /*全局变量,用来存放获取的CPU使用信息*/
enum{ /*进程树表信息*/
col_name = 0,
col_stat,
col_pid,
col_ppid,
col_nice,
col_mem,
n_cols1
};
void
on_new_item_activate (GtkMenuItem *menuitem,
gpointer user_data)
{
}
void
on_open_item_activate (GtkMenuItem *menuitem,
gpointer user_data)
{
}
void
on_t_1_activate (GtkMenuItem *menuitem,
gpointer user_data)
{
}
void
on_c_1_activate (GtkMenuItem *menuitem,
gpointer user_data)
{
}
void
on_p_1_activate (GtkMenuItem *menuitem,
gpointer user_data)
{
}
void
on_d_1_activate (GtkMenuItem *menuitem,
gpointer user_data)
{
}
void
on_about1_activate (GtkMenuItem *menuitem,
gpointer user_data)
{
}
void on_reboot_item_activate(GtkMenuItem *menuitem, gpointer user_data) /*重启*/
{
system("reboot");
}
void on_shutdown_item_activate(GtkMenuItem *menuitem, gpointer user_data) /*关机*/
{
system("halt");
}
struct Process{ /*存放进程信息*/
char pid[5];
char comm[20];
char state[20];
char mem[20];
char ppid[20];;
char nice[20];
} pro[200];
float mm=0,shao=0;
float util=0;
void model_data_new1(GtkTreeModel* store1,
const gchar* name, const gchar* stat, const gchar *pid,const gchar *ppid,const gchar *nice, const gchar *mem) { /*建立树表数据*/
GtkTreeIter iter;
gtk_list_store_append(GTK_LIST_STORE(store1), &iter);
gtk_list_store_set(GTK_LIST_STORE(store1), &iter,
col_name, name,
col_stat, stat,
col_pid, pid,
col_ppid,ppid,
col_nice,nice,
col_mem, mem,
-1);
}
GtkTreeModel* create_model1() { /*创建树表*/
GtkListStore *store1;
store1 = gtk_list_store_new (n_cols1,G_TYPE_STRING,G_TYPE_STRING,G_TYPE_STRING,G_TYPE_STRING,G_TYPE_STRING,G_TYPE_STRING,G_TYPE_STRING);
return GTK_TREE_MODEL(store1);
}
void arrange_tree_view1(GtkWidget* view) {
GtkCellRenderer* renderer;
renderer = gtk_cell_renderer_text_new ();
gtk_tree_view_insert_column_with_attributes(
GTK_TREE_VIEW(view), -1, "进程名\t", renderer, "text", col_name, NULL);
gtk_tree_view_insert_column_with_attributes(
GTK_TREE_VIEW(view), -1, "状态\t", renderer, "text", col_stat, NULL);
gtk_tree_view_insert_column_with_attributes(
GTK_TREE_VIEW(view), -1, "pid\t", renderer, "text", col_pid, NULL);
gtk_tree_view_insert_column_with_attributes(
GTK_TREE_VIEW(view), -1, "ppid\t", renderer, "text", col_ppid, NULL);
gtk_tree_view_insert_column_with_attributes(
GTK_TREE_VIEW(view), -1, "nice\t", renderer, "text", col_nice, NULL);
gtk_tree_view_insert_column_with_attributes(
GTK_TREE_VIEW(view), -1, "内存\t", renderer, "text", col_mem, NULL);
}
int select_name(char name[]) /*查找文件名*/
{ int i;
for(i=0;name[i]!=0;i++)
if(isalpha(name[i])||name[i]=='.')
return 0;
return 1;
}
int show_pro_info(void){ /*显示进程信息函数*/
DIR *dir;
struct dirent *ptr;
int fd,tt;
int i=0;
int j;
int total=0;
char path_statm[50];
char path_status[50];
char path_stat[50];
char buffer[100];
char buf[100];
dir=opendir("/proc");
while((ptr=readdir(dir))!=NULL){
if(select_name(ptr->d_name)){
strcpy(pro[i].pid,ptr->d_name);
i++;
total++;
}
}
closedir(dir);
for(i=0;i<total;i++){
strcpy(path_statm,"/proc/");
strcpy(path_status,"/proc/");
strcat(path_statm,pro[i].pid);
strcat(path_statm,"/statm");
strcat(path_status,pro[i].pid);
strcat(path_status,"/status");
strcpy(path_stat,"/proc/");
strcat(path_stat,pro[i].pid);
strcat(path_stat,"/stat");
fd=open(path_status,O_RDONLY);
tt=read(fd,buffer,100);
strtok(buffer,":");
strcpy(pro[i].comm,strtok(NULL,"\n"));
strtok(NULL,":");
strcpy(pro[i].state,strtok(NULL,"\n"));
strtok(NULL,":");
strtok(NULL,"\n");
strtok(NULL,":");
strtok(NULL,"\n");
strtok(NULL,":");
strtok(NULL,"\n");
strtok(NULL,":");
strcpy(pro[i].ppid,strtok(NULL,"\n"));
close(fd);
fd=open(path_statm,O_RDONLY);
tt=read(fd,buf,100);
strcpy(pro[i].mem,strtok(buf," "));
strcat(pro[i].mem,"kb");
close(fd);
fd=open(path_stat,O_RDONLY);
tt=read(fd,buf,100);
strtok(buf," ");
for(j=0;j<17;j++){
strtok(NULL," ");
}
strcpy(pro[i].nice,strtok(NULL," "));
close(fd);
}
return total;
}
gint show_proc(GtkWidget *treeview1){ /*将进程信息插入树表中*/
int i,num;
GtkTreeModel *store = create_model1();
gtk_tree_view_set_model ( GTK_TREE_VIEW(treeview1), store);
num=show_pro_info();
/*if(num) gtk_list_store_clear(GTK_LIST_STORE(store));*/
for(i=0;i<num;i++)
model_data_new1(store,pro[i].comm,pro[i].state,pro[i].pid,pro[i].ppid,pro[i].nice,pro[i].mem); //
g_object_unref( store );
return 1;
}
gint get_cpu_util(GtkWidget *label29){ /*计算cpu使用率*/
int fp;
char *buf,*name,*buf1;
buf=(char *)malloc(256*sizeof(char));
buf1=(char *)malloc(256*sizeof(char));
name=(char *)malloc(256*sizeof(char));
long user1,system1,nice1,idle1,iowait1,irq1,softirq1;
long sum1,sum2;
fp=open("/proc/stat",O_RDONLY);
read(fp,buf,200);
sscanf(buf,"%s %ld %ld %ld %ld %ld %ld %ld",name,&user1,&system1,&nice1,&idle1,&iowait1,&irq1,&softirq1);
sum1=user1+nice1+system1+idle1+iowait1+irq1+softirq1;
close(fp);
sum2=user2+nice2+system2+idle2+iowait2+irq2+softirq2;
util=(float)100.0*(user2-user1+system2-system1)/(sum2-sum1);
user2=user1;system2=system1;nice2=nice1;idle2=idle1;iowait2=iowait1;irq2=irq1;softirq2=softirq1;
printf("%f",util);
sprintf(buf1,"cpu使用率:%f %%\n\n\n\n\n内存信息:",util);
close(fp);
gtk_label_set_text(GTK_LABEL(label29),buf1);
free(buf);
free(buf1);
free(name);
return 1;
}
gint show_mem_info(GtkWidget *label30){ /*显示内存信息*/
int fd;
char *buf;
buf=(char *)malloc(500*sizeof(char));
fd=open("/proc/meminfo",O_RDONLY);
read(fd,buf,350);
close(fd);
gtk_label_set_text(GTK_LABEL(label30),buf);
free(buf);
return 1;
}
gint timeout_callback(GtkWidget *clocker){ /*显示系统开启时间,运行时间和当前时间*/
int fd;
int time_run;
int hour,minute,second;
time_t now,timep;
struct tm *l_time;
char *buf,*buf1;
buf=(char *)malloc(256*sizeof(char));
buf1=(char *)malloc(256*sizeof(char));
now=time((time_t*)NULL);
l_time=localtime(&now);
sprintf(buf,"现在时间:%d:%d:%d",l_time->tm_hour,l_time->tm_min,l_time->tm_sec);
strcat(buf,"\n系统开启时间:");
fd=open("/proc/uptime",O_RDONLY);
read(fd,buf1,100);
time_run=atoi(strtok(buf1,"."));
time(&timep);
timep-=time_run;
strcat(buf,ctime(&timep));
strcat(buf,"系统运行时间:");
fd=open("/proc/uptime",O_RDONLY);
read(fd,buf1,100);
time_run=atoi(strtok(buf1,"."));
hour=time_run/3600;
minute=(time_run-hour*3600)/60;
second=time_run%60;
sprintf(buf1,"%d:%d:%d",hour,minute,second);
strcat(buf,buf1);
gtk_label_set_text(GTK_LABEL(clocker),buf);
free(buf);
free(buf1);
return 1;
}
void show_sys_info(GtkWidget * text){ /*读取主机名和系统版本号*/
int fd0;
int fd1;
int fd2;
char *buf,*buf1;
buf=(char *)malloc(2000*sizeof(char));
buf1=(char *)malloc(2000*sizeof(char));
strcat(buf,"\n主机名:");
fd0=open("/proc/sys/kernel/hostname",O_RDONLY);
read(fd0,buf1,2000);
close(fd0);
strcat(buf,buf1);
strcat(buf,"\n系统版本号:");
fd1=open("/proc/sys/kernel/osrelease",O_RDONLY);
read(fd1,buf1,2000);
close(fd1);
strcat(buf,buf1);
strcat(buf,"\ncpu型号和主频:");
fd2=open("/proc/cpuinfo",O_RDONLY);
read(fd2,buf1,165);
close(fd2);
strcat(buf,buf1);
gtk_label_set_text(GTK_LABEL(text),buf);
free(buf);
free(buf1);
}
gint show_disk(GtkWidget * label32){ /*显示硬盘信息函数*/
int fd;
char *buf;
buf=(char *)malloc(2000*sizeof(char));
fd=open("/proc/partitions",O_RDONLY);
read(fd,buf,1000);
close(fd);
gtk_label_set_text(GTK_LABEL(label32),buf);
return 1;
}
void kill_button( GtkWidget *widget,gpointer data){ /*杀死进程函数*/
char *s=(char *)malloc(2000*sizeof(char));
char *t=data;
strcpy(s,"kill -9 ");
strcat(s,t);
printf("%s",s);
system(s);
}
void cx_button( GtkWidget *widget,gpointer data){ /*查询进程信息函数*/
GtkWidget *main_window;
GtkWidget *label;
GtkWidget *button,*vbox;
char *entry_text=data;
char path_status[50];
int fd;
char buf[200];
main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(G_OBJECT(main_window), "delete_event", G_CALLBACK(gtk_main_quit), NULL);
gtk_window_set_title(GTK_WINDOW(main_window), "进程信息!");
gtk_window_set_default_size(GTK_WINDOW(main_window), 200,200);
label=gtk_label_new(NULL);
gtk_widget_show(label);
button=gtk_button_new_with_label("结束进程");
vbox=gtk_vbox_new(FALSE,10);
gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 10);
gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 10);
gtk_container_add(GTK_CONTAINER(main_window), vbox);
printf("entry_text=%s",entry_text);
strcpy(path_status,"/proc/");
strcat(path_status,entry_text);
strcat(path_status,"/status");
fd=open(path_status,O_RDONLY);
read(fd,buf,200);
g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(kill_button),entry_text);
gtk_label_set_text(GTK_LABEL(label),buf);
gtk_widget_show_all(main_window);
gtk_main();
}
void cb_button(GtkWidget *widget, gpointer data){ /*查询进程窗口*/
GtkWidget *main_window; //主窗口对象
GtkWidget *hbox, *button, *editor;
char *entry_text;
main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(G_OBJECT(main_window), "delete_event", G_CALLBACK(gtk_main_quit), NULL);
gtk_window_set_title(GTK_WINDOW(main_window), "查询进程!");
gtk_window_set_default_size(GTK_WINDOW(main_window), 100,50);
button=gtk_button_new_with_label("查询");
editor=gtk_entry_new();
hbox = gtk_hbox_new(FALSE, 10);
gtk_box_pack_start(GTK_BOX(hbox), editor, TRUE, TRUE, 10);
gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 10);
gtk_container_add(GTK_CONTAINER(main_window), hbox);
entry_text = gtk_entry_get_text (GTK_ENTRY (editor));
g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(cx_button),entry_text);
gtk_widget_show_all(main_window);
gtk_main();
}
void create_window2(gpointer data){ /*创建进程详细信息窗口*/
GtkWidget *main_window;
GtkWidget *vbox,*button1,*button2;
char *pid=data;
main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(G_OBJECT(main_window), "delete_event", G_CALLBACK(gtk_main_quit), NULL);
gtk_window_set_title(GTK_WINDOW(main_window),"进程操作" );
gtk_window_set_default_size(GTK_WINDOW(main_window), 50,50);
button1=gtk_button_new_with_label("进程信息");
button2=gtk_button_new_with_label("结束进程");
vbox=gtk_vbox_new(FALSE,10);
gtk_box_pack_start(GTK_BOX(vbox), button1, TRUE, TRUE, 10);
gtk_box_pack_start(GTK_BOX(vbox), button2, FALSE, FALSE, 10);
gtk_container_add(GTK_CONTAINER(main_window), vbox);
g_signal_connect(G_OBJECT(button1), "clicked", G_CALLBACK(cx_button),pid);
g_signal_connect(G_OBJECT(button2), "clicked", G_CALLBACK(kill_button),pid);
gtk_widget_show_all(main_window);
gtk_main();
}
gboolean /*树表选中函数*/
view_selection_func (GtkTreeSelection *selection,
GtkTreeModel *model,
GtkTreePath *path,
gboolean path_currently_selected,
gpointer userdata)
{
GtkTreeIter iter;
char *pid;
if (gtk_tree_model_get_iter(model, &iter, path))
{
gtk_tree_model_get(model, &iter, col_pid, &pid, -1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -