process_status

0.0.2 • Public • Published

process_status

I needed to display process information on a status page in a dashboard. From the command line I would type something like:

$ ps -ef | grep httpd
root      3199     1  0 Sep17 ?        00:00:00 /usr/sbin/httpd
apache    3252  3199  0 Sep17 ?        00:00:00 /usr/sbin/httpd
apache    3253  3199  0 Sep17 ?        00:00:00 /usr/sbin/httpd
apache    3254  3199  0 Sep17 ?        00:00:00 /usr/sbin/httpd
apache    3255  3199  0 Sep17 ?        00:00:00 /usr/sbin/httpd
apache    3256  3199  0 Sep17 ?        00:00:00 /usr/sbin/httpd
apache    3257  3199  0 Sep17 ?        00:00:00 /usr/sbin/httpd
apache    3258  3199  0 Sep17 ?        00:00:00 /usr/sbin/httpd
apache    3259  3199  0 Sep17 ?        00:00:00 /usr/sbin/httpd
swright  10615  8615  0 14:51 pts/1    00:00:00 grep httpd

The process_status utility runs something like that with some formatting options and returns an object containing something like the following:

{ grep_str: 'httpd',
  user_format: [],
  pid: '3199',
  pgid: '3199',
  format_str: '%U%p%r%a',
  processes:
   [ 'root      3199  3199 /usr/sbin/httpd',
     'apache    3252  3199 /usr/sbin/httpd',
     'apache    3253  3199 /usr/sbin/httpd',
     'apache    3254  3199 /usr/sbin/httpd',
     'apache    3255  3199 /usr/sbin/httpd',
     'apache    3256  3199 /usr/sbin/httpd',
     'apache    3257  3199 /usr/sbin/httpd',
     'apache    3258  3199 /usr/sbin/httpd',
     'apache    3259  3199 /usr/sbin/httpd' ],
  format_array: [ 'user', 'pid', 'pgid', 'args' ],
  formatted_process_list:
   [ [ 'root', '3199', '3199', '/usr/sbin/httpd' ],
     [ 'apache', '3252', '3199', '/usr/sbin/httpd' ],
     [ 'apache', '3253', '3199', '/usr/sbin/httpd' ],
     [ 'apache', '3254', '3199', '/usr/sbin/httpd' ],
     [ 'apache', '3255', '3199', '/usr/sbin/httpd' ],
     [ 'apache', '3256', '3199', '/usr/sbin/httpd' ],
     [ 'apache', '3257', '3199', '/usr/sbin/httpd' ],
     [ 'apache', '3258', '3199', '/usr/sbin/httpd' ],
     [ 'apache', '3259', '3199', '/usr/sbin/httpd' ] ] }

You can then use the display the data using the processes array or for prettier output you can use the formatted_process_list array to generate a table and the format_array to generate table headers.

Usage:

/**
*  Finds a process based on the grep string
*  
*  @param  grep_str    a string that will be used to grep the process list
*  @param   user_format     an array of friendly strings that will be used to 
*                           get string formatters from the formatters array
*  @param   cb              callback  
*/
get_status(grep_str, user_format, cb)

To generate the formatted_process_list array

/**
* @param   status      A status object created by get_status
* @param   cb          A callback function
*/
process_status_data(status, cb)

See the test.js file for an implementation example.

Readme

Keywords

none

Package Sidebar

Install

npm i process_status

Weekly Downloads

2

Version

0.0.2

License

none

Last publish

Collaborators

  • rhythmicdevil