Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1924332
  • 博文数量: 261
  • 博客积分: 8073
  • 博客等级: 中将
  • 技术积分: 2363
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-10 15:23
文章分类

全部博文(261)

文章存档

2013年(1)

2012年(1)

2011年(50)

2010年(34)

2009年(4)

2008年(17)

2007年(55)

2006年(99)

分类:

2010-09-14 14:11:09

(一)获取磁盘的动态使用状况

其实系统的动态信息都可以从Solaris里的kstat工具得到。vmstat之类的工具都是基于kstat。有一篇很好的文章讲了kstat的基本知识和数据结构,要想写出适合自己要求的系统监控代码,应该先看看这篇文章:http://developers.sun.com/solaris/articles/kstatc.html

以下的例子就是根据这篇文章里的代码改写的。

#include #include
#include
#include
#define SLEEPTIME 5

unsigned int sleep(unsigned int);
void my_io_display(char *, char *, kstat_io_t);

main(int argc, char *argv[])
{
  kstat_ctl_t *kc;
  kstat_t *ksp;
  kstat_io_t kio;
  
  kc = kstat_open();
  
  
  while(1) {
    if(kstat_chain_update(kc))
    ftf(stderr, "<>n");
    for (ksp = kc->kc_chain; ksp != NULL; ksp = ksp->ks_next) {
      if (ksp->ks_type == KSTAT_TYPE_IO &;& strcmp(ksp->ks_class,"disk") == 0) {
        kstat_read(kc, ksp, &kio);
        my_io_display(ksp->ks_name, ksp->ks_class, kio);
      }
    }
    sleep(SLEEPTIME);
  } /* while(1) */

}

void my_io_display(char *devname, char *class, kstat_io_t k)
{
printf("Name: %s Class: %sn",devname,class);
printf("tnumber of bytes read %lldn", k.nread);
printf("tnumber of bytes written %lldn", k.nwritten);
printf("tnumber of read operations %dn", k.reads);
printf("tnumber of write operations %dn", k.writes);
}

(二)获取内存的动态使用状况

这个稍微麻烦点儿,因为kstat的vminfo保存的是自系统启动以来累计信息,因此要计算两次采样时间之间的差值。

#include
#include
#include
#include
#include

#define SLEEPTIME 5
#define pgtok(a) ((a) * (pagesize >> 10))

unsigned int sleep(unsigned int);
main(int argc, char *argv[])
{
  kstat_ctl_t *kc;
  kstat_t *ksp;
  vminfo_t vminfo, old_vminfo;
  int pagesize = sysconf(_SC_PAGESIZE);
  
  kc = kstat_open();
  ksp = kstat_lookup(kc, NULL, -1,"vminfo");
  kstat_read(kc, ksp, &old_vminfo);
  sleep(SLEEPTIME);
  
  while(1) {
    if(kstat_chain_update(kc))
    fprintf(stderr, "<>n");
    ksp = kstat_lookup(kc, NULL, -1,"vminfo");
    kstat_read(kc, ksp, &vminfo);
    
    
    printf("Free memory: %uKBn", pgtok((vminfo.freemem - old_vminfo.freemem)/SLEEPTIME));
    printf("Swap allocated: %uKBn", pgtok((vminfo.swap_alloc - old_vminfo.swap_alloc)/SLEEPTIME));
    printf("Swap available: %ukBn", pgtok((vminfo.swap_avail - old_vminfo.swap_avail)/SLEEPTIME));
    
    kstat_read(kc, ksp, &old_vminfo); /* you can use memcpy instead to store the old */
    printf("n");
    sleep(SLEEPTIME);
  } /* while(1) */
  
}

你还可以用kstat写一个cpu使用状况的程序,这个就交给你自己去联系了,呵呵。如果想不出来,就去看看mpstat的源码。

(三)获取物理内存信息。

这很简单,参考opensolaris里的prtconf.c。用sysconf系统调用。

#include
#include
#include

main()
{
  uint32_t pagesize;
  uint64_t phy_pages;
  pagesize = (uint32_t) sysconf (_SC_PAGE_SIZE);
  phy_pages = (uint64_t) sysconf (_SC_PHYS_PAGES);
  printf("Physical memory: %lu MBn", (pagesize * phy_pages) >> 20);
}

(四)获取文件系统的使用情况。

这个就用不到kstat了。需要读取mount table, 结合statvfs调用。

#include
#include
#include
#include
#include
#include

#define MNTTAB "/etc/mnttab"

main()
{
  struct statvfs fsstat;
  struct mnttab my_mnttab;
  FILE *fp;
  
  fp = fopen(MNTTAB, "r");
  if (fp == NULL) {
   printf("Unable to open mount table.n");
   exit(-1);
  }

          while (getmntent(fp, &my_mnttab) == 0){

  if (strncmp(my_mnttab.mnt_fstype, "ufs", 3) != 0)
  continue;
  
  memset(&fsstat, 0, sizeof(struct statvfs));
    if (statvfs(my_mnttab.mnt_mountp, &fsstat) == 0) {
    printf("File System: %sn", my_mnttab.mnt_mountp);
    printf("tSpace Total: %lu KBn",
    fsstat.f_blocks * (fsstat.f_frsize >> 10) );
    printf("tSpace Available: %lu KBn",
    fsstat.f_bavail * (fsstat.f_frsize >> 10) );
    printf("n");
    }
  } /* end of while */
}

(五)获取网卡信息和IP地址

这个嘛,开个socket,再用ioctl把有关的信息拿到。opensolaris里有个程序,现成的,连测试的main函数都替你写好了:

阅读(4237) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~