Back to index

nagios-plugins  1.4.16
pst3.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002 * 
00003 * pst3
00004 * 
00005 * License: GPL
00006 * Copyright (c) 2008 Nagios Plugin Development Team
00007 * 
00008 * Description:
00009 * 
00010 * This file contains the pst3 executable. This is a replacement ps command
00011 * for Solaris to get output which provides a long argument listing, which
00012 * is not possible with the standard ps command (due to truncation). /usr/ucb/ps
00013 * also has issues where some fields run into each other.
00014 * 
00015 * This executable works by reading process address structures, so needs
00016 * to be executed as root
00017 *
00018 * Originally written by R.W.Ingraham
00019 * Rewritten by Duncan Ferguson (Altinity Ltd, June 2008)
00020 *   The rewrite was necessary as /dev/kmem is not available within
00021 *   non-global zones on Solaris 10
00022 *
00023 *   Details for rewrite came from
00024 *    source of /usr/ucb/ps on Solaris:
00025 *     http://cvs.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/ucbcmd/ps/ps.c#argvoff
00026 *    usenet group posting
00027 *     http://groups.google.com/group/comp.unix.solaris/tree/browse_frm/month/2001-09/bfa40c08bac819a2?rnum=141&_done=%2Fgroup%2Fcomp.unix.solaris%2Fbrowse_frm%2Fmonth%2F2001-09%3F
00028 * 
00029 * This program is free software: you can redistribute it and/or modify
00030 * it under the terms of the GNU General Public License as published by
00031 * the Free Software Foundation, either version 3 of the License, or
00032 * (at your option) any later version.
00033 * 
00034 * This program is distributed in the hope that it will be useful,
00035 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00036 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00037 * GNU General Public License for more details.
00038 * 
00039 * You should have received a copy of the GNU General Public License
00040 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00041 * 
00042 *****************************************************************************/
00043 
00044 #include <stdio.h>
00045 #include <stdlib.h>
00046 #include <string.h>
00047 #include <unistd.h>
00048 #include <dirent.h>
00049 #include <errno.h>
00050 #include <fcntl.h>
00051 #include <procfs.h>
00052 #include <sys/types32.h>
00053 
00054 /*
00055  *  Constants
00056  */
00057 
00058 #define PROC_DIR  "/proc"
00059 #define ARGS            30
00060 
00061 /*
00062  *  Globals
00063  */
00064 
00065 static char *        szProg;
00066 
00067 /*
00068  *  Prototypes
00069  */
00070 void usage();
00071 
00072 /*----------------------------------------------------------------------------*/
00073 
00074 int main (int argc, char **argv)
00075 {
00076   DIR *procdir;
00077   struct dirent *proc;
00078   char ps_name[ARGS];
00079   char as_name[ARGS];
00080   psinfo_t psinfo;
00081 
00082   /* Set our program name global */
00083   if ((szProg = strrchr(argv[0], '/')) != NULL)
00084     szProg++;
00085   else
00086     szProg = argv[0];
00087 
00088   /* if given any parameters, print out help */
00089   if(argc > 1) {
00090     (void)usage();
00091     exit(1);
00092   }
00093 
00094   /* Make sure that our euid is root */
00095   if (geteuid() != 0)
00096   {
00097     fprintf(stderr, "%s: This program can only be run by the root user!\n", szProg);
00098     exit(1);
00099   }
00100 
00101   if ((procdir = opendir(PROC_DIR)) == NULL) {
00102     fprintf(stderr, "%s: cannot open PROC directory %s\n", szProg, PROC_DIR);
00103     exit(1);
00104   }
00105 
00106   /* Display column headings */
00107   printf("%c %5s %5s %5s %6s %6s %4s %s %s\n",
00108     'S',
00109     "UID",
00110     "PID",
00111     "PPID",
00112     "VSZ",
00113     "RSS",
00114     "%CPU",
00115     "COMMAND",
00116     "ARGS"
00117   );
00118 
00119   /* Zip through all of the process entries */
00120   while((proc = readdir(procdir))) {
00121     int ps_fd;
00122     int as_fd;
00123     off_t argoff;
00124     int i;
00125     char *args;
00126     char *procname;
00127     char *ptr;
00128     int argslen;
00129     uintptr_t args_addr;;
00130     uintptr_t *args_vecs;;
00131     int args_count;
00132 
00133     if(proc->d_name[0] == '.')
00134       continue;
00135 
00136     sprintf(ps_name,"%s/%s/%s",PROC_DIR,proc->d_name,"psinfo");
00137     sprintf(as_name,"%s/%s/%s",PROC_DIR,proc->d_name,"as");
00138 try_again:
00139     if((ps_fd = open(ps_name, O_RDONLY)) == -1)
00140       continue;
00141 
00142     if((as_fd = open(as_name, O_RDONLY)) == -1)
00143       continue;
00144 
00145     if(read(ps_fd, &psinfo, sizeof(psinfo)) != sizeof(psinfo)) {
00146       int err = errno;
00147       close(ps_fd);
00148       close(as_fd);
00149       if(err == EAGAIN) goto try_again;
00150       if(err != ENOENT)
00151         fprintf(stderr, "%s: read() on %s: %s\n", szProg,
00152           ps_name, strerror(err));
00153       continue;
00154     }
00155     close(ps_fd);
00156 
00157     /* system process, ignore since the previous version did */
00158     if(
00159       psinfo.pr_nlwp == 0 ||
00160       strcmp(psinfo.pr_lwp.pr_clname, "SYS") == 0
00161     ) {
00162       continue;
00163     }
00164 
00165     /* get the procname to match previous versions */
00166     procname = strdup(psinfo.pr_psargs);
00167     if((ptr = strchr(procname, ' ')) != NULL)
00168         *ptr = '\0';
00169     if((ptr = strrchr(procname, '/')) != NULL)
00170         ptr++;
00171     else
00172         ptr = procname;
00173 
00174     /*
00175      * print out what we currently know
00176      */
00177     printf("%c %5d %5d %5d %6lu %6lu %4.1f %s ",
00178       psinfo.pr_lwp.pr_sname,
00179       psinfo.pr_euid,
00180       psinfo.pr_pid,
00181       psinfo.pr_ppid,
00182       psinfo.pr_size,
00183       psinfo.pr_rssize,
00184       ((float)(psinfo.pr_pctcpu) / 0x8000 * 100.0),
00185       ptr
00186     );
00187     free(procname);
00188 
00189     /*
00190      * and now for the command line stuff
00191      */
00192 
00193     args_addr = psinfo.pr_argv;
00194     args_count = psinfo.pr_argc;
00195     args_vecs = malloc(args_count * sizeof(uintptr_t));
00196 
00197     if(psinfo.pr_dmodel == PR_MODEL_NATIVE) {
00198       /* this process matches target process */
00199       pread(as_fd,args_vecs, args_count * sizeof(uintptr_t),
00200         args_addr);
00201     } else {
00202       /* this process is 64bit, target process is 32 bit*/
00203       caddr32_t *args_vecs32 = (caddr32_t *)args_vecs;
00204       pread(as_fd,args_vecs32,args_count * sizeof(caddr32_t),
00205         args_addr);
00206       for (i=args_count-1;i>=0;--i)
00207         args_vecs[i]=args_vecs32[i];
00208     }
00209 
00210     /*
00211      * now read in the args - if what we read in fills buffer
00212      * resize buffer and reread that bit again
00213      */
00214     argslen=ARGS;
00215     args=malloc(argslen+1);
00216     for(i=0;i<args_count;i++) {
00217       memset(args,'\0',argslen+1);
00218       if(pread(as_fd, args, argslen, args_vecs[i]) <= 0) {
00219         break;
00220       }
00221       args[argslen]='\0';
00222       if(strlen(args) == argslen){
00223         argslen += ARGS;
00224         args = realloc(args, argslen + 1);
00225         i--;
00226         continue;
00227       }
00228       printf(" %s", args);
00229     }
00230     free(args_vecs);
00231     free(args);
00232     close(as_fd);
00233     printf("\n");
00234   }
00235 
00236   (void) closedir(procdir);
00237 
00238   return (0);
00239 }
00240 
00241 /*----------------------------------------------------------------------------*/
00242 
00243 void usage() {
00244   printf("%s: Help output\n\n", szProg);
00245   printf("If this program is given any arguments, this help is displayed.\n");
00246   printf("This command is used to print out the full command line for all\n");
00247   printf("running processes because /usr/bin/ps is limited to 80 chars and\n");
00248   printf("/usr/ucb/ps can merge columns together.\n\n");
00249   printf("Columns are:\n");
00250   printf("\tS        - State of process - see 'ps' man page\n");
00251   printf("\tUID      - UID of the process owner\n");
00252   printf("\tPID      - PID of the process\n");
00253   printf("\tPPID     - PID of the parent process\n");
00254   printf("\tVSZ      - Virtual memory usage (kilobytes)\n");
00255   printf("\tRSS      - Real memory usage (kilobytes)\n");
00256   printf("\t%%CPU     - CPU usage\n");
00257   printf("\tCOMMAND  - Command being run\n");
00258   printf("\tARGS     - Full command line with arguements\n");
00259   return;
00260 }