c_helpers.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include "c_helpers.h"
  2. GETFUNC(cpu)
  3. GETFUNC(disk)
  4. GETFUNC(diskadapter)
  5. GETFUNC(diskpath)
  6. GETFUNC(fcstat)
  7. GETFUNC(logicalvolume)
  8. GETFUNC(memory_page)
  9. GETFUNC(netadapter)
  10. GETFUNC(netbuffer)
  11. GETFUNC(netinterface)
  12. GETFUNC(pagingspace)
  13. GETFUNC(process)
  14. GETFUNC(thread)
  15. GETFUNC(volumegroup)
  16. double get_partition_mhz(perfstat_partition_config_t pinfo) {
  17. return pinfo.processorMHz;
  18. }
  19. char *get_ps_hostname(perfstat_pagingspace_t *ps) {
  20. return ps->u.nfs_paging.hostname;
  21. }
  22. char *get_ps_filename(perfstat_pagingspace_t *ps) {
  23. return ps->u.nfs_paging.filename;
  24. }
  25. char *get_ps_vgname(perfstat_pagingspace_t *ps) {
  26. return ps->u.lv_paging.vgname;
  27. }
  28. time_t boottime()
  29. {
  30. register struct utmpx *utmp;
  31. setutxent();
  32. while ( (utmp = getutxent()) != NULL ) {
  33. if (utmp->ut_type == BOOT_TIME) {
  34. return utmp->ut_tv.tv_sec;
  35. }
  36. }
  37. endutxent();
  38. return -1;
  39. }
  40. struct fsinfo *get_filesystem_stat(struct fsinfo *fs_all, int n) {
  41. if (!fs_all) return NULL;
  42. return &(fs_all[n]);
  43. }
  44. int get_mounts(struct vmount **vmountpp) {
  45. int size;
  46. struct vmount *vm;
  47. int nmounts;
  48. size = BUFSIZ;
  49. while (1) {
  50. if ((vm = (struct vmount *)malloc((size_t)size)) == NULL) {
  51. perror("malloc failed");
  52. exit(-1);
  53. }
  54. if ((nmounts = mntctl(MCTL_QUERY, size, (caddr_t)vm)) > 0) {
  55. *vmountpp = vm;
  56. return nmounts;
  57. } else if (nmounts == 0) {
  58. size = *(int *)vm;
  59. free((void *)vm);
  60. } else {
  61. free((void *)vm);
  62. return -1;
  63. }
  64. }
  65. }
  66. void fill_fsinfo(struct statfs statbuf, struct fsinfo *fs) {
  67. fsblkcnt_t freeblks, totblks, usedblks;
  68. fsblkcnt_t tinodes, ninodes, ifree;
  69. uint cfactor;
  70. if (statbuf.f_blocks == -1) {
  71. fs->totalblks = 0;
  72. fs->freeblks = 0;
  73. fs->totalinodes = 0;
  74. fs->freeinodes = 0;
  75. return;
  76. }
  77. cfactor = statbuf.f_bsize / 512;
  78. fs->freeblks = statbuf.f_bavail * cfactor;
  79. fs->totalblks = statbuf.f_blocks * cfactor;
  80. fs->freeinodes = statbuf.f_ffree;
  81. fs->totalinodes = statbuf.f_files;
  82. if (fs->freeblks < 0)
  83. fs->freeblks = 0;
  84. }
  85. int getfsinfo(char *fsname, char *devname, char *host, char *options, int flags, int fstype, struct fsinfo *fs) {
  86. struct statfs statbuf;
  87. int devname_size = strlen(devname);
  88. int fsname_size = strlen(fsname);
  89. char buf[BUFSIZ];
  90. char *p;
  91. if (fs == NULL) {
  92. return 1;
  93. }
  94. for (p = strtok(options, ","); p != NULL; p = strtok(NULL, ","))
  95. if (strcmp(p, "ignore") == 0)
  96. return 0;
  97. if (*host != 0 && strcmp(host, "-") != 0) {
  98. sprintf(buf, "%s:%s", host, devname);
  99. devname = buf;
  100. }
  101. fs->devname = (char *)calloc(devname_size+1, 1);
  102. fs->fsname = (char *)calloc(fsname_size+1, 1);
  103. strncpy(fs->devname, devname, devname_size);
  104. strncpy(fs->fsname, fsname, fsname_size);
  105. fs->flags = flags;
  106. fs->fstype = fstype;
  107. if (statfs(fsname,&statbuf) < 0) {
  108. return 1;
  109. }
  110. fill_fsinfo(statbuf, fs);
  111. return 0;
  112. }
  113. struct fsinfo *get_all_fs(int *rc) {
  114. struct vmount *mnt;
  115. struct fsinfo *fs_all;
  116. int nmounts;
  117. *rc = -1;
  118. if ((nmounts = get_mounts(&mnt)) <= 0) {
  119. perror("Can't get mount table info");
  120. return NULL;
  121. }
  122. fs_all = (struct fsinfo *)calloc(sizeof(struct fsinfo), nmounts);
  123. while ((*rc)++, nmounts--) {
  124. getfsinfo(vmt2dataptr(mnt, VMT_STUB),
  125. vmt2dataptr(mnt, VMT_OBJECT),
  126. vmt2dataptr(mnt, VMT_HOST),
  127. vmt2dataptr(mnt, VMT_ARGS),
  128. mnt->vmt_flags,
  129. mnt->vmt_gfstype,
  130. &fs_all[*rc]);
  131. mnt = (struct vmount *)((char *)mnt + mnt->vmt_length);
  132. }
  133. return fs_all;
  134. }