Displaying memory

From: Russell Johnson <rkj_at_beta.loyno.edu>
Date: Thu, 27 Mar 1997 09:30:36 -0600 (CST)

Thanks again to All!
There were many many responses.
I liked these two the best from
From: "Randy M. Hayman" <haymanr_at_icefog.sois.alaska.edu>
to C-programs
for physical memory display
cut-------------------------------------
#include <stdio.h>
#include <sys/sysinfo.h>

int status, *int_buff;

main()
{
  if ( -1 == (status = getsysinfo(GSI_PHYSMEM, &int_buff, sizeof(int_buff), 0,
0)) )
    fprintf(stdout, "error %d getting GSI_PHYSMEM\n", status);
  else
    fprintf(stdout, "Physical memory in use: %d MB\n", ((int)int_buff+512)/1024);
}
cut--------------------------------------

for filesystem display
cut-------------------------------------

/*======================================================================*/
/* filesystems.c provide a listing of filesystems */
/*======================================================================*/
/*
 *
 * All rights reserved.
 * (c) 1996, Randy M. Hayman and the University of Alaska
 *
 * Modifications:
 *
 * 961230 rmhayman added disklabels, unfortunately needs to be root
 * or we need to add g+r to /dev/r*z[0-9]* devices
 * now needs to be compiled with the following:
 * cc -std1 -o filesystems -DDKTYPENAMES filesystems.c
 * with optional debugging flags [-DDEBUG[0-4]]
 *
 * 961224 rmhayman all functional except regcomp() and friends
 *
 * 961217 rmhayman initial file creation.
 * compile with the following:
 * cc -std1 -o filesystems [-DDEBUG[12]] filesystems.c
 *
 */

#include <stdio.h> /* for standard in, out, err */
#include <errno.h> /* for errno definitions */
#include <regex.h> /* needed for regcomp() and friends */
#include <sys/ioctl.h> /* for ioctl() */
#include <sys/disklabel.h> /* for disk label info (i.e. disklabel -r command)*/
#include <sys/types.h> /* for typedefs used */
#include <sys/fs_types.h> /* for mounted filesystem types array mnt_names[] */
#include <sys/mount.h> /* for MNT*, MOUNT_* flags and MNAMELEN definition */
#include <fcntl.h> /* for open() */
#include <sys/stat.h> /* for stat struct */
#include <unistd.h> /* for execl() */
#include <dirent.h> /* for getdirentries() */

#define ADVFS_DMNS_DIR "/etc/fdmns/"
#define PATTERN_LEN (80) /* max length of input pattern to run through
regcomp() and friends */
#define MESG_LEN (128) /* max length of error message returned from
regerror() */
#define MAX_BYTES (4*8192)/* max number of bytes to get from getdirentries()
    */

#define showme_s(p,x) fprintf(stdout, "%d %s> %s: %s = %s\n", __LINE__, __FILE__, p, #x, x);
#define showme_d(p,x) fprintf(stdout, "%d %s> %s: %s = %ld\n", __LINE__, __FILE__, p, #x, x);

#define usage fprintf(stderr, "Usage: %s %s\n", pname, usage_text);
char *pname, usage_text[80] = "[-a(dvfs) -c(dfs) -n(fs) -p(rocfs) -u(fs) -d <regexp> -i(gnore case)]";

char separater_line[132] =
"------------------------------------------------------------------------------------------------------------------------------------";

char header0[132] = " # type disk label type disk filesystem KBtotal KBavail inodes available mount-point";

struct statfs *mountbuf;
int mounts, status, i, j, kbtotal, kbavail, flag = 0;
char *domain, domain_dir[MNAMELEN], *device, *tmpdev, mntinfo[32],
dirinfo[132];
char filesystem[MNAMELEN], remote_dir[MNAMELEN], *remote_p,
dkpath[MNAMELEN];
struct disklabel dklabel;
int all = TRUE, advfs = FALSE, cdfs = FALSE, nfs = FALSE, procfs = FALSE,
ufs = FALSE;
int ignore = FALSE, pattern = FALSE, message_len;
char *s, reg_pattern[PATTERN_LEN], message[MESG_LEN];
regex_t regex_buf;
char find_command[132+132], pathname[132], filename[80];

main(int argc, char *argv[])
{
  pname = argv[0];

  if (0 == (mounts = getmntinfo(&mountbuf, MNT_NOWAIT))) {
    fprintf(stderr, "%s: Unable to get mount data\n", pname);
    exit(1);
  }
#ifdef DEBUG0
  showme_d(pname, mounts);
#endif

  while (--argc > 0 && (*++argv)[0] == '-' ) {
    for (s = argv[0]+1; *s != '\0'; s++)
      switch( *s) {

      case 'a': /* display Advfs filesystems */
        advfs++;
        all = FALSE;
        break;

      case 'c': /* display CDFS (ISO 9660) filesystems */
        cdfs++;
        all = FALSE;
        break;

      case 'n': /* display NFS and NFS v3 filesystems */
        nfs++;
        all = FALSE;
        break;

      case 'p': /* display the procfs filesystem */
        procfs++;
        all = FALSE;
        break;

      case 'u': /* display UFS filesystems */
        ufs++;
        all = FALSE;
        break;

      case 'd': /* display data on these directories */
        if (--argc <=0) {
          fprintf(stderr, "-d requires another argument - a regular expression.\n");
          usage;
          exit(1);
        }
        pattern++;
        if (PATTERN_LEN <= strlen(*++argv)) {
          fprintf(stderr, "<pattern> must be less than %d characters.\n", PATTERN_LEN);
          usage;
          exit(1);
        }
        sprintf(reg_pattern, "%s", *argv);
        break;

      case 'i': /* ignore case in matching regular expressions */
        ignore++;
        break;

      default:
        usage;
        exit(1);
    }
  }

  if (ignore && !pattern)
    fprintf(stderr, "Ignoring ambiguous -i flag...\n");

  if (TRUE == all) {
    advfs++;
    cdfs++;
    nfs++;
    procfs++;
    ufs++;
  }

  fprintf(stdout, "%s\n", header0);
  fprintf(stdout, "%s\n", separater_line);

  for (i = 0, j = 0; i < mounts ; i++) {
    get_device_name();
    get_mount_info();
#ifdef DEBUG1
    showme_s(pname, device);
    showme_s(pname, &mntinfo[0]);
#endif

    if ((FALSE == advfs) && (mountbuf[i].f_type == MOUNT_MSFS))
      continue; /* do not display advfs data */
    if ((FALSE == cdfs) && (mountbuf[i].f_type == MOUNT_CDFS))
      continue; /* do not display cdfs data */
    if ((FALSE == nfs) && ((mountbuf[i].f_type == MOUNT_NFS) ||
(mountbuf[i].f_type == MOUNT_NFS3)))
      continue; /* do not display any nfs data */
    if ((FALSE == procfs) && (mountbuf[i].f_type == MOUNT_PROCFS))
      continue; /* do not display procfs data */
    if ((FALSE == ufs) && (mountbuf[i].f_type == MOUNT_UFS))
      continue; /* do not display ufs data */

    if ((0 != mountbuf[i].f_files) || all) {
      kbtotal = (ulong)mountbuf[i].f_blocks * (ulong)mountbuf[i].f_fsize / 1024;
      kbavail = (ulong)mountbuf[i].f_bavail * (ulong)mountbuf[i].f_fsize / 1024;
      j++;
      fprintf(stdout, "%2d: %6s %-7s %-7s %-4s %-5s %-25s %9d %9d %9d %9d %-20s ",/* %-20s*/
        j,
        mnt_names[mountbuf[i].f_type],
        device,
        ((mountbuf[i].f_type == MOUNT_MSFS) || (mountbuf[i].f_type ==
MOUNT_UFS))?dklabel.d_packname:"",
        ((mountbuf[i].f_type == MOUNT_MSFS) || (mountbuf[i].f_type ==
MOUNT_UFS))?dktypenames[dklabel.d_type]:"",
        ((mountbuf[i].f_type == MOUNT_MSFS) || (mountbuf[i].f_type ==
MOUNT_UFS))?dklabel.d_typename:"",
        ((mountbuf[i].f_type == MOUNT_NFS) || (mountbuf[i].f_type ==
MOUNT_NFS3))?remote_p:&filesystem[0],
        kbtotal,
        kbavail<0?0:kbavail, /* some CDs have less than zero available!? */
        mountbuf[i].f_files,
        mountbuf[i].f_ffree,
        &mountbuf[i].f_mntonname[0]);
/* (mountbuf[i].f_type == MOUNT_MSFS)?&mntinfo[0]:""); /* Advfs ID */

      if (pattern) {
        strcpy(&pathname[0], &mountbuf[i].f_mntonname[0]);
        sprintf(&filename[0], "%s/%s", &pathname[0], reg_pattern);
        if (access(&filename[0], R_OK)) {
          showme_s("access", &filename[0]);
/* continue; /* can't read file */
        }
        sprintf(&find_command[0],"/usr/bin/ls -l %s | /usr/bin/awk '{print \"\t\t\" $5 \" \t \" $9}' ",
          &filename[0]);
        system(&find_command[0]);
      }

      fprintf(stdout, "\n%s\n", separater_line);
    }
  }
  exit(0);
}


/* -- -- return any information asked for via the regular expression from this mount point -- -- */
static caught_sig, catch_code, pid;

void sig_handler(int sig)
{
  caught_sig = sig;
}

get_directory_info(void)
{
/*
struct dirent *dir_ent;
int dir_fd, offset, cnt;
char dir_buf[MAX_BYTES];
long entloc = 0;
*/

void (*old_quit)(int), (*old_intr)(int);

  if (ignore) /* ignore case in regular expression matching? */
    status = regcomp(&regex_buf, reg_pattern, REG_EXTENDED | REG_ICASE);
  else
    status = regcomp(&regex_buf, reg_pattern, REG_EXTENDED);

  if (status) { /* oops, had a problem in regcomp(), what is it... */
    message_len = regerror(status, &regex_buf, &message[0], MESG_LEN);
    fprintf(stderr, "%s", message);
    if (MESG_LEN < message_len)
      fprintf(stderr, "...\n"); /* lost some text */
    else
      fprintf(stderr, "\n"); /* got it all */
  }

  sprintf(&find_command[0],"/usr/bin/ls -l %s/%s | /usr/bin/awk '{print \"\t\t\" $5 \" \t \" $9}' ",
    &mountbuf[i].f_mntonname[0], reg_pattern);
  system(&find_command[0]);

  return;

  /* catch some signals which exec'd prog. may ignore */
  old_quit = signal(SIGQUIT, sig_handler);
  old_intr = signal(SIGINT, sig_handler);
  caught_sig = 0;
  if (pid = fork())
    while (wait(&catch_code) != pid); /* parent */
  else { /* child */
    fprintf(stdout, "<%d><%d> \t %s\n", getppid(), getpid(),&find_command[0]);
    fflush(stdout);
    system(&find_command[0]);
    exit(0);
  }

  signal(SIGQUIT, old_quit);
  signal(SIGINT, old_intr);

  if (caught_sig)
    kill(0, caught_sig);

  return(catch_code?0:1);
/*
  if (-1 == (dir_fd = open( &mountbuf[i].f_mntonname[0], O_RDONLY))) {
    perror(&mountbuf[i].f_mntonname[0]);
    exit(1);
  }
  else {
    if (-1 == (status = getdirentries (dir_fd, &dir_buf[0], MAX_BYTES, &entloc)))
{
      perror(dir_fd);
      exit(1);
    }
    else {
      strcpy(&dirinfo[0], "got direntries successfully");
    }
  }

  fprintf(stdout, "%s\n", &dirinfo[0]);
  for (offset = 0, dir_ent = (struct dirent *)dir_buf, cnt = 1 ; offset < status
;
    offset += dir_ent->d_reclen, dir_ent = (struct dirent *)&dir_buf[offset],
cnt++) {
    if (dir_ent->d_namlen)
      fprintf(stdout, "(%d) %s \t %s", cnt, dir_ent->d_name, cnt%7?"":"\n");
  }
  fprintf(stdout, "\nlisted %d files\n", --cnt);
*/
  return;
}


/* -- -- return any additional information about this mount table entry that we
wish to see -- -- */
get_mount_info(void)
{
  return;

  if (!strcmp(mnt_names[mountbuf[i].f_type],mnt_names[MOUNT_MSFS])) { /* advfs
*/
    sprintf(&mntinfo[0], "(%lx.%0lx.%lx)",
      mountbuf[i].mount_info.msfs_args.id.id1,
      mountbuf[i].mount_info.msfs_args.id.id2,
      mountbuf[i].mount_info.msfs_args.id.tag);
    return;
  }
  return;
}


/* -- -- return the device name of this filesystem, typically in a format like
rz[a-h][0-4096][a-h] -- -- */
get_device_name(void)
{
int fd;
DIR *dir_fd;
struct dirent *dir_entry;
/*
struct stat *statbuf;
*/

  strcpy(&filesystem[0], &mountbuf[i].f_mntfromname[0]);
  device = mountbuf[i].f_mntfromname;

  if (!strcmp(mnt_names[mountbuf[i].f_type],mnt_names[MOUNT_MSFS])) { /* advfs
*/
    domain = (char *)strtok(device, "#");
    strcpy(&domain_dir[0], ADVFS_DMNS_DIR);
    strcat(&domain_dir[0], domain);
    if( NULL == (dir_fd = opendir(&domain_dir[0]))) {
      fprintf(stderr, "Couldn't open %s (%d)\n", &domain_dir[0], dir_fd);
      return;
    }
    while( NULL != (dir_entry = readdir(dir_fd))) {
      strcpy(device, dir_entry->d_name);
#ifdef DEBUG2
      showme_s("get_device_name", &domain_dir[0]);
      showme_s("get_device_name:readdir", dir_entry->d_name);
      showme_s("get_device_name", device);
#endif
      if( !strcmp(dir_entry->d_name, "."))
        continue;
      if( !strcmp(dir_entry->d_name, ".."))
        continue;

      strcpy(&dkpath[0], "/dev/r");
      strcat(&dkpath[0], device);
      if(0 > (fd = open(&dkpath[0], O_RDONLY)))
        perror(&dkpath[0]);
      if(0 > (ioctl(fd, DIOCGDINFO, &dklabel)))
        perror(&dkpath[0]);
#ifdef DEBUG3
      showme_s("ioctl", dktypenames[dklabel.d_type]);
      showme_s("ioctl", dklabel.d_typename);
      showme_s("ioctl", dklabel.d_packname);
#endif

/***************************************************************************
 * if we care to get a struct stat to look at, this block can be uncommented
 * and the statbuf typedef above needs to be uncommented also...
 *
 * if( 0 > lstat( dir_entry->d_name, statbuf))
 * continue;
 ***************************************************************************/

      closedir(dir_fd);
      close(fd);
    }
    closedir(dir_fd);
  }

  if (!strcmp(mnt_names[mountbuf[i].f_type],mnt_names[MOUNT_UFS])) { /* ufs */
    device = (char *)strtok(device, "/");
    device = (char *)strtok(NULL, "/");
    strcpy(&dkpath[0], "/dev/r"); /* character device please */
    strcat(&dkpath[0], device);
    if(0 > (fd = open(&dkpath[0], O_RDONLY)))
      perror(&dkpath[0]);
    if(0 > (ioctl(fd, DIOCGDINFO, &dklabel)))
      perror(&dkpath[0]);
#ifdef DEBUG3
    showme_s("ioctl", dktypenames[dklabel.d_type]);
    showme_s("ioctl", dklabel.d_typename);
    showme_s("ioctl", dklabel.d_packname);
#endif
    close(fd);
  }

  if (!strcmp(mnt_names[mountbuf[i].f_type],mnt_names[MOUNT_PROCFS])) { /*
procfs*/
    device = (char *)strtok(device, "/");
  }

  if (!strcmp(mnt_names[mountbuf[i].f_type],mnt_names[MOUNT_CDFS])) { /* cdfs
*/
    device = (char *)strtok(device, "/");
    device = (char *)strtok(NULL, "/");
  }

  if (!strcmp(mnt_names[mountbuf[i].f_type],mnt_names[MOUNT_NFS])) { /* nfs */
    if (NULL == (tmpdev = (char *)strrchr(device, '_at_')))
      tmpdev = (char *)strtok(device, ":"); /* no '_at_', check for ':'*/
    else
      tmpdev++; /* get rid of '_at_' sign */
    tmpdev = (char *)strtok(tmpdev, "."); /* get rid of domain */
    device = tmpdev; /* device is hostname */
    strcpy(&remote_dir[0], &filesystem[0]);
    if (NULL == (remote_p = (char *)strrchr(&remote_dir[0], ':')))
      remote_p = (char *)strtok(&remote_dir[0], "_at_"); /* no ':', check for '_at_'*/
    else
      remote_p++; /* get rid of ':' */
  }

  if (!strcmp(mnt_names[mountbuf[i].f_type],mnt_names[MOUNT_NFS3])) { /* nfsv3
*/
    if (NULL == (tmpdev = (char *)strrchr(device, '_at_')))
      tmpdev = (char *)strtok(device, ":"); /* no '_at_', check for ':'*/
    else
      tmpdev++; /* get rid of '_at_' sign */
    tmpdev = (char *)strtok(tmpdev, "."); /* get rid of domain */
    device = tmpdev; /* device is hostname */
    strcpy(&remote_dir[0], &filesystem[0]);
    if (NULL == (remote_p = (char *)strrchr(&remote_dir[0], ':')))
      remote_p = (char *)strtok(&remote_dir[0], "_at_"); /* no ':', check for '_at_'*/
    else
      remote_p++; /* get rid of ':' */
  }

  return;
}
cut-----------------------------------------------------
thanks again guys,
RKJ
Received on Thu Mar 27 1997 - 17:13:46 NZST

This archive was generated by hypermail 2.4.0 : Wed Nov 08 2023 - 11:53:36 NZDT