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