xymon

Hex Artifact Content
Login

Artifact 77503539377988a68a9374d4b2cf9fbfb2dab53b382e03b5a735dc5a0b885fd0:


0000: 2f 2a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  /*--------------
0010: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0020: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0030: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0040: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2a 2f  --------------*/
0050: 0a 2f 2a 20 58 79 6d 6f 6e 20 6d 65 6d 6f 72 79  ./* Xymon memory
0060: 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 6f   information too
0070: 6c 20 66 6f 72 20 4f 70 65 6e 42 53 44 2e 20 20  l for OpenBSD.  
0080: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
0090: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a                 *
00a0: 2f 0a 2f 2a 20 54 68 69 73 20 74 6f 6f 6c 20 72  /./* This tool r
00b0: 65 74 72 69 65 76 65 73 20 69 6e 66 6f 72 6d 61  etrieves informa
00c0: 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 74  tion about the t
00d0: 6f 74 61 6c 20 61 6e 64 20 66 72 65 65 20 52 41  otal and free RA
00e0: 4d 2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  M.              
00f0: 2a 2f 0a 2f 2a 20 20 20 20 20 20 20 20 20 20 20  */./*           
0100: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
0110: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
0120: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
0130: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
0140: 20 2a 2f 0a 2f 2a 20 43 6f 70 79 72 69 67 68 74   */./* Copyright
0150: 20 28 43 29 20 32 30 30 35 2d 32 30 31 31 20 48   (C) 2005-2011 H
0160: 65 6e 72 69 6b 20 53 74 6f 72 6e 65 72 20 3c 68  enrik Storner <h
0170: 65 6e 72 69 6b 40 68 73 77 6e 2e 64 6b 3e 20 20  enrik@hswn.dk>  
0180: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
0190: 20 20 2a 2f 0a 2f 2a 20 20 20 20 20 20 20 20 20    */./*         
01a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
01b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
01c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
01d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
01e0: 20 20 20 2a 2f 0a 2f 2a 20 54 68 69 73 20 70 72     */./* This pr
01f0: 6f 67 72 61 6d 20 69 73 20 72 65 6c 65 61 73 65  ogram is release
0200: 64 20 75 6e 64 65 72 20 74 68 65 20 47 4e 55 20  d under the GNU 
0210: 47 65 6e 65 72 61 6c 20 50 75 62 6c 69 63 20 4c  General Public L
0220: 69 63 65 6e 73 65 20 28 47 50 4c 29 2c 20 20 20  icense (GPL),   
0230: 20 20 20 20 2a 2f 0a 2f 2a 20 76 65 72 73 69 6f      */./* versio
0240: 6e 20 32 2e 20 53 65 65 20 74 68 65 20 66 69 6c  n 2. See the fil
0250: 65 20 22 43 4f 50 59 49 4e 47 22 20 66 6f 72 20  e "COPYING" for 
0260: 64 65 74 61 69 6c 73 2e 20 20 20 20 20 20 20 20  details.        
0270: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
0280: 20 20 20 20 20 2a 2f 0a 2f 2a 20 20 20 20 20 20       */./*      
0290: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
02a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
02b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
02c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
02d0: 20 20 20 20 20 20 2a 2f 0a 2f 2a 2d 2d 2d 2d 2d        */./*-----
02e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
02f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0300: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0310: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0320: 2d 2d 2d 2d 2d 2d 2d 2a 2f 0a 0a 73 74 61 74 69  -------*/..stati
0330: 63 20 63 68 61 72 20 72 63 73 69 64 5b 5d 20 3d  c char rcsid[] =
0340: 20 22 24 49 64 24 22 3b 0a 0a 23 69 6e 63 6c 75   "$Id$";..#inclu
0350: 64 65 20 3c 73 79 73 2f 70 61 72 61 6d 2e 68 3e  de <sys/param.h>
0360: 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 74  .#include <sys/t
0370: 79 70 65 73 2e 68 3e 0a 23 69 6e 63 6c 75 64 65  ypes.h>.#include
0380: 20 3c 73 79 73 2f 73 79 73 63 74 6c 2e 68 3e 0a   <sys/sysctl.h>.
0390: 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 73 77  #include <sys/sw
03a0: 61 70 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c  ap.h>.#include <
03b0: 73 79 73 2f 76 6d 6d 65 74 65 72 2e 68 3e 0a 23  sys/vmmeter.h>.#
03c0: 69 6e 63 6c 75 64 65 20 3c 75 6e 69 73 74 64 2e  include <unistd.
03d0: 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64  h>.#include <std
03e0: 6c 69 62 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20  lib.h>.#include 
03f0: 3c 73 74 64 69 6f 2e 68 3e 0a 0a 69 6e 74 20 6d  <stdio.h>..int m
0400: 61 69 6e 28 69 6e 74 20 61 72 67 63 2c 20 63 68  ain(int argc, ch
0410: 61 72 20 2a 61 72 67 76 5b 5d 29 0a 7b 0a 09 69  ar *argv[]).{..i
0420: 6e 74 20 68 77 5f 70 68 79 73 6d 65 6d 5b 5d 20  nt hw_physmem[] 
0430: 3d 20 7b 20 43 54 4c 5f 48 57 2c 20 48 57 5f 50  = { CTL_HW, HW_P
0440: 48 59 53 4d 45 4d 20 7d 3b 0a 09 75 6e 73 69 67  HYSMEM };..unsig
0450: 6e 65 64 20 69 6e 74 20 70 68 79 73 6d 65 6d 3b  ned int physmem;
0460: 0a 0a 09 69 6e 74 20 68 77 5f 70 61 67 65 73 69  ...int hw_pagesi
0470: 7a 65 5b 5d 20 3d 20 7b 20 43 54 4c 5f 48 57 2c  ze[] = { CTL_HW,
0480: 20 48 57 5f 50 41 47 45 53 49 5a 45 20 7d 3b 0a   HW_PAGESIZE };.
0490: 09 69 6e 74 20 70 61 67 65 73 69 7a 65 3b 0a 0a  .int pagesize;..
04a0: 09 69 6e 74 20 76 6d 5f 76 6d 74 6f 74 61 6c 5b  .int vm_vmtotal[
04b0: 5d 20 3d 20 7b 20 43 54 4c 5f 56 4d 2c 20 56 4d  ] = { CTL_VM, VM
04c0: 5f 4d 45 54 45 52 20 7d 3b 0a 09 73 74 72 75 63  _METER };..struc
04d0: 74 20 76 6d 74 6f 74 61 6c 20 76 6d 64 61 74 61  t vmtotal vmdata
04e0: 3b 0a 0a 09 73 69 7a 65 5f 74 20 6c 65 6e 3b 0a  ;...size_t len;.
04f0: 09 69 6e 74 20 72 65 73 75 6c 74 3b 0a 09 69 6e  .int result;..in
0500: 74 20 73 77 61 70 63 6f 75 6e 74 2c 20 69 3b 0a  t swapcount, i;.
0510: 09 73 74 72 75 63 74 20 73 77 61 70 65 6e 74 20  .struct swapent 
0520: 2a 73 77 61 70 6c 69 73 74 2c 20 2a 73 3b 0a 09  *swaplist, *s;..
0530: 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 73 77  unsigned long sw
0540: 61 70 74 6f 74 61 6c 2c 20 73 77 61 70 75 73 65  aptotal, swapuse
0550: 64 3b 0a 0a 09 6c 65 6e 20 3d 20 73 69 7a 65 6f  d;...len = sizeo
0560: 66 28 70 68 79 73 6d 65 6d 29 3b 0a 09 72 65 73  f(physmem);..res
0570: 75 6c 74 20 3d 20 73 79 73 63 74 6c 28 68 77 5f  ult = sysctl(hw_
0580: 70 68 79 73 6d 65 6d 2c 20 73 69 7a 65 6f 66 28  physmem, sizeof(
0590: 68 77 5f 70 68 79 73 6d 65 6d 29 20 2f 20 73 69  hw_physmem) / si
05a0: 7a 65 6f 66 28 2a 68 77 5f 70 68 79 73 6d 65 6d  zeof(*hw_physmem
05b0: 29 2c 20 26 70 68 79 73 6d 65 6d 2c 20 26 6c 65  ), &physmem, &le
05c0: 6e 2c 20 4e 55 4c 4c 2c 20 30 29 3b 0a 09 69 66  n, NULL, 0);..if
05d0: 20 28 72 65 73 75 6c 74 20 21 3d 20 30 29 20 72   (result != 0) r
05e0: 65 74 75 72 6e 20 31 3b 0a 0a 09 6c 65 6e 20 3d  eturn 1;...len =
05f0: 20 73 69 7a 65 6f 66 28 70 61 67 65 73 69 7a 65   sizeof(pagesize
0600: 29 3b 0a 09 72 65 73 75 6c 74 20 3d 20 73 79 73  );..result = sys
0610: 63 74 6c 28 68 77 5f 70 61 67 65 73 69 7a 65 2c  ctl(hw_pagesize,
0620: 20 73 69 7a 65 6f 66 28 68 77 5f 70 61 67 65 73   sizeof(hw_pages
0630: 69 7a 65 29 20 2f 20 73 69 7a 65 6f 66 28 2a 68  ize) / sizeof(*h
0640: 77 5f 70 61 67 65 73 69 7a 65 29 2c 20 26 70 61  w_pagesize), &pa
0650: 67 65 73 69 7a 65 2c 20 26 6c 65 6e 2c 20 4e 55  gesize, &len, NU
0660: 4c 4c 2c 20 30 29 3b 0a 09 69 66 20 28 72 65 73  LL, 0);..if (res
0670: 75 6c 74 20 21 3d 20 30 29 20 72 65 74 75 72 6e  ult != 0) return
0680: 20 31 3b 0a 0a 09 6c 65 6e 20 3d 20 73 69 7a 65   1;...len = size
0690: 6f 66 28 76 6d 64 61 74 61 29 3b 0a 09 72 65 73  of(vmdata);..res
06a0: 75 6c 74 20 3d 20 73 79 73 63 74 6c 28 76 6d 5f  ult = sysctl(vm_
06b0: 76 6d 74 6f 74 61 6c 2c 20 73 69 7a 65 6f 66 28  vmtotal, sizeof(
06c0: 76 6d 5f 76 6d 74 6f 74 61 6c 29 20 2f 20 73 69  vm_vmtotal) / si
06d0: 7a 65 6f 66 28 2a 76 6d 5f 76 6d 74 6f 74 61 6c  zeof(*vm_vmtotal
06e0: 29 2c 20 26 76 6d 64 61 74 61 2c 20 26 6c 65 6e  ), &vmdata, &len
06f0: 2c 20 4e 55 4c 4c 2c 20 30 29 3b 0a 0a 09 2f 2a  , NULL, 0);.../*
0700: 20 47 65 74 20 73 77 61 70 20 73 74 61 74 69 73   Get swap statis
0710: 74 69 63 73 20 2a 2f 0a 09 73 77 61 70 63 6f 75  tics */..swapcou
0720: 6e 74 20 3d 20 73 77 61 70 63 74 6c 28 53 57 41  nt = swapctl(SWA
0730: 50 5f 4e 53 57 41 50 2c 20 4e 55 4c 4c 2c 20 30  P_NSWAP, NULL, 0
0740: 29 3b 0a 09 73 77 61 70 6c 69 73 74 20 3d 20 28  );..swaplist = (
0750: 73 74 72 75 63 74 20 73 77 61 70 65 6e 74 20 2a  struct swapent *
0760: 29 6d 61 6c 6c 6f 63 28 73 77 61 70 63 6f 75 6e  )malloc(swapcoun
0770: 74 20 2a 20 73 69 7a 65 6f 66 28 73 74 72 75 63  t * sizeof(struc
0780: 74 20 73 77 61 70 65 6e 74 29 29 3b 0a 09 72 65  t swapent));..re
0790: 73 75 6c 74 20 3d 20 73 77 61 70 63 74 6c 28 53  sult = swapctl(S
07a0: 57 41 50 5f 53 54 41 54 53 2c 20 73 77 61 70 6c  WAP_STATS, swapl
07b0: 69 73 74 2c 20 73 77 61 70 63 6f 75 6e 74 29 3b  ist, swapcount);
07c0: 0a 09 73 20 3d 20 73 77 61 70 6c 69 73 74 3b 20  ..s = swaplist; 
07d0: 73 77 61 70 74 6f 74 61 6c 20 3d 20 73 77 61 70  swaptotal = swap
07e0: 75 73 65 64 20 3d 20 30 3b 0a 09 66 6f 72 20 28  used = 0;..for (
07f0: 69 20 3d 20 30 2c 20 73 20 3d 20 73 77 61 70 6c  i = 0, s = swapl
0800: 69 73 74 3b 20 28 69 20 3c 20 73 77 61 70 63 6f  ist; (i < swapco
0810: 75 6e 74 29 3b 20 69 2b 2b 2c 20 73 2b 2b 29 20  unt); i++, s++) 
0820: 7b 0a 09 09 69 66 20 28 73 2d 3e 73 65 5f 66 6c  {...if (s->se_fl
0830: 61 67 73 20 26 20 53 57 46 5f 45 4e 41 42 4c 45  ags & SWF_ENABLE
0840: 29 20 7b 0a 09 09 09 73 77 61 70 74 6f 74 61 6c  ) {....swaptotal
0850: 20 2b 3d 20 73 2d 3e 73 65 5f 6e 62 6c 6b 73 3b   += s->se_nblks;
0860: 0a 09 09 09 73 77 61 70 75 73 65 64 20 20 2b 3d  ....swapused  +=
0870: 20 73 2d 3e 73 65 5f 69 6e 75 73 65 3b 0a 09 09   s->se_inuse;...
0880: 7d 0a 09 7d 0a 09 66 72 65 65 28 73 77 61 70 6c  }..}..free(swapl
0890: 69 73 74 29 3b 0a 09 2f 2a 20 73 77 61 70 20 73  ist);../* swap s
08a0: 74 61 74 73 20 61 72 65 20 69 6e 20 64 69 73 6b  tats are in disk
08b0: 20 62 6c 6f 63 6b 73 20 6f 66 20 35 31 32 20 62   blocks of 512 b
08c0: 79 74 65 73 2c 20 73 6f 20 64 69 76 69 64 65 20  ytes, so divide 
08d0: 62 79 20 32 20 66 6f 72 20 4b 42 20 61 6e 64 20  by 2 for KB and 
08e0: 31 30 32 34 20 66 6f 72 20 4d 42 20 2a 2f 0a 09  1024 for MB */..
08f0: 73 77 61 70 74 6f 74 61 6c 20 2f 3d 20 28 32 2a  swaptotal /= (2*
0900: 31 30 32 34 29 3b 0a 09 73 77 61 70 75 73 65 64  1024);..swapused
0910: 20 2f 3d 20 28 32 2a 31 30 32 34 29 3b 0a 0a 09   /= (2*1024);...
0920: 2f 2f 20 70 72 69 6e 74 66 28 22 50 61 67 65 73  // printf("Pages
0930: 69 7a 65 3a 25 64 5c 6e 22 2c 20 70 61 67 65 73  ize:%d\n", pages
0940: 69 7a 65 29 3b 0a 09 70 72 69 6e 74 66 28 22 54  ize);..printf("T
0950: 6f 74 61 6c 3a 25 64 5c 6e 22 2c 20 28 70 68 79  otal:%d\n", (phy
0960: 73 6d 65 6d 20 2f 20 28 31 30 32 34 20 2a 20 31  smem / (1024 * 1
0970: 30 32 34 29 29 29 3b 0a 09 70 72 69 6e 74 66 28  024)));..printf(
0980: 22 46 72 65 65 3a 25 64 5c 6e 22 2c 20 28 70 61  "Free:%d\n", (pa
0990: 67 65 73 69 7a 65 20 2f 20 31 30 32 34 29 2a 28  gesize / 1024)*(
09a0: 76 6d 64 61 74 61 2e 74 5f 66 72 65 65 20 2f 20  vmdata.t_free / 
09b0: 31 30 32 34 29 29 3b 0a 09 70 72 69 6e 74 66 28  1024));..printf(
09c0: 22 53 77 61 70 74 6f 74 61 6c 3a 25 6c 75 5c 6e  "Swaptotal:%lu\n
09d0: 22 2c 20 73 77 61 70 74 6f 74 61 6c 29 3b 0a 09  ", swaptotal);..
09e0: 70 72 69 6e 74 66 28 22 53 77 61 70 75 73 65 64  printf("Swapused
09f0: 3a 25 6c 75 5c 6e 22 2c 20 73 77 61 70 75 73 65  :%lu\n", swapuse
0a00: 64 29 3b 0a 0a 09 72 65 74 75 72 6e 20 30 3b 0a  d);...return 0;.
0a10: 7d 0a 0a                                         }..