Visit Our UNIX and Linux User Community


parsing currently running processes


 
Thread Tools Search this Thread
Top Forums Programming parsing currently running processes
# 1  
Old 10-11-2006
parsing currently running processes

Hey guys,
I'm writing a monitoring program that reads the pattern and the max and min number of instances of a process and then proceeds to parse the currently running processes for the pattern.

I just want to know how I should go about this. I'll give you an idea of the flow of the program:
1. daemonise /*this is yet to be implemented*/
2. read the process info file to build a list of the process pattern, min number and max number of instances and the rate of scanning for the pattern (in minutes).
3. loop forever and compare the processes in the list to the currently running processes and throw the appropriate alerts (or whatever)

It is point #3 that has me a bit confused. Should I read the /proc into another list, loop for each process pattern in the original list over the proc list? Or should I not read the /proc into a list at all, and just iterate over the list and read the /proc everytime (considering that /proc is in memory, that shouldn't take too much time either).

Currently the OSes that should work are Solaris and Linux as I have easy access to both, I will probably extend this to HP-UX later.
# 2  
Old 10-11-2006
Quote:
Originally Posted by blowtorch
Hey guys,
I'm writing a monitoring program that reads the pattern and the max and min number of instances of a process and then proceeds to parse the currently running processes for the pattern.
Parse the process? Do you mean the cmdline that started the process ?

Quote:
Originally Posted by blowtorch
2. read the process info file to build a list of the process pattern, min number and max number of instances and the rate of scanning for the pattern (in minutes).
3. loop forever and compare the processes in the list to the currently running processes and throw the appropriate alerts (or whatever)

It is point #3 that has me a bit confused. Should I read the /proc into another list, loop for each process pattern in the original list over the proc list? Or should I not read the /proc into a list at all, and just iterate over the list and read the /proc everytime (considering that /proc is in memory, that shouldn't take too much time either).
Keep a tab on the pids, i.e. maintain a list (best would hashtable).
If you iterate through /proc always, you might parse some long running processes again and again.
- For each iteration on the list, you can figure out the process is still running or not. If no ,then remove the entry. If yes, go ahead with your processing. And retain the entry. I dont think you want to raise an alert for the same process again and again.
For the next iteration against /proc, collect the new pids only. The old ones are the ones you processed already.

Quote:
Originally Posted by blowtorch
Currently the OSes that should work are Solaris and Linux as I have easy access to both, I will probably extend this to HP-UX later.
I thought Solaris did not have the /proc file system. Hmm..
# 3  
Old 10-11-2006
Quote:
Originally Posted by vino
Parse the process? Do you mean the cmdline that started the process ?
Yes. I don't mean parse as such, just verify that the processes that have the cmdline patterns specified in the input file are running (with the correct number of instances).
Quote:
Originally Posted by vino
Keep a tab on the pids, i.e. maintain a list (best would hashtable).
That is a good idea, but even though most of the processes would be long running (such as webservers, app servers and such), I still feel I should check against the command line.
Quote:
Originally Posted by vino
I thought Solaris did not have the /proc file system. Hmm..
In fact Solaris has a very 'pure' /proc. It only contains information for currently running processes.
# 4  
Old 10-12-2006
# 5  
Old 10-12-2006
Hi Hitori,
I don't want to know how to read the process details. I am doing that already. My program will have a list of, lets say, command lines patterns, that it has to match against currently running processes. It will do this in an infinite loop, raising alerts if any of the process patterns are not found in the list of running processes or if the number of matches are too few or too many.

It may have to go through the list of running processes every minute (maybe, depending on the configuration). So, my question is whether it would be better to create a list of command line entries from the /proc every minute and go through my list and match (the entire list of current processes would be processes for every entry in my list), or should I go through the all /proc entries for each of the entries in my list.

The first method involves creating a list of an unknown size but issuing the /proc reading commands (readdir, open, read, stat) just once. The second method requires the readdir, open, read commands to be executed for the entire /proc filesystem for each process pattern in my list.
# 6  
Old 10-12-2006
Quote:
Originally Posted by blowtorch
It may have to go through the list of running processes every minute (maybe, depending on the configuration). So, my question is whether it would be better to create a list of command line entries from the /proc every minute and go through my list and match (the entire list of current processes would be processes for every entry in my list), or should I go through the all /proc entries for each of the entries in my list.

The first method involves creating a list of an unknown size but issuing the /proc reading commands (readdir, open, read, stat) just once. The second method requires the readdir, open, read commands to be executed for the entire /proc filesystem for each process pattern in my list.
Why can't you use 1st method without creating a list? You can test each /proc entry immediatelly against your list
# 7  
Old 10-12-2006
That becomes the same as the second method. Here's what I'm trying to say in pseudocode:
method 1 - read /proc into a list
Code:
while readdir of /proc returns valid entries; do
        read the /proc/<pid>/psinfo file into a struct
        append the struct to a list of such structs
done
for each element in list; do
        for each element in list of structs; do
                compare element against the ps cmd line from struct
                if match, increase count of pattern instances
        done
        if count of pattern instances
                below lower threshold
                        raise alert
                above upper threshold
                        raise alert
                between the correct bounds
                        do nothing
done

method 2 - read /proc for each entry in the list
Code:
for each element in list; do
        while readdir of /proc returns valid entries; do
                read the /proc/<pid>/psinfo file into a struct
                compare element against the ps cmd line from struct
                if match, increase count of pattern instances
        done
        if count of pattern instances 
                below lower threshold
                        raise alerts
                above upper threshold
                        raise alerts
                between the correct bounds
                        do nothing
done


Previous Thread | Next Thread
Test Your Knowledge in Computers #285
Difficulty: Easy
AIX was the first operating system to have a proc file system.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Many processes running at the same time

Hello everybody , I launched cron to execute a task every hour but the job takes more than hour that's why I'm getting more than 1000 cron processes running at the same time !!! My question is how to tell cron not to execute unless the job terminated in order to have only one process running .... (14 Replies)
Discussion started by: beautymind
14 Replies

2. Solaris

Sendmail processes not running

Hi All! I am trying to get sendmail to work but unsuccessfull...when I run ps -ef | grep sendmail root 10578 10561 0 11:01:24 pts/1 0:00 grep sendmail I do not see its processes When I run the following commands: bash-3.00# svcs sendmail svcs: Pattern 'sendmail' doesn't match... (9 Replies)
Discussion started by: fretagi
9 Replies

3. Linux

Running processes

Hi guys is it normal to have 5-10 cron/syslog processes running... in my case i got 10 cron process running. (4 Replies)
Discussion started by: batas
4 Replies

4. Solaris

Running processes on GZ/LZ

Hi guys just a question is it normal to see running process on a non-global zone in the global zone... processes such as cron. (3 Replies)
Discussion started by: batas
3 Replies

5. Shell Programming and Scripting

how to know the running processes.

Hi can anybody help me regarding this.. i want know the output of ps -ef with explanation. how can we know the running processess. this is the output of ps -elf F S UID PID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME CMD 19 T root 0 0 0 0 SY ... (1 Reply)
Discussion started by: rajesh_pola
1 Replies

6. Shell Programming and Scripting

Need help with running processes script

I'm doing a script with the Shell. I need that it only show the number of running processes. Ex: echo "There are `command` running processes" Thnx! Pd: Sorry the idiom. I'm spanish. (5 Replies)
Discussion started by: Ikebana
5 Replies

7. Shell Programming and Scripting

Need help with running processes script

I'm doing a script with the Shell. I need that it only show the number of running processes. Ex: echo "There are `command` running processes" Thnx! Pd: Sorry the idiom. I'm spanish. (2 Replies)
Discussion started by: Ikebana
2 Replies

8. Solaris

About running processes in background

Hi, I need to establish a procedure that will start an application in background each time my remote Solaris server is (re)started. This would be a kind of daemon. I am no sysadmin expert, so I am looking for pointers. How should I proceed? What are the main steps? Thanks, JVerstry (9 Replies)
Discussion started by: JVerstry
9 Replies

9. Programming

getting certain info for all processes currently running

i am writing a utility that displays information about all the running processes in the /proc directory. I do not know how to get the following information for each of the processes in the /proc directory: -effective user id -effective user name -effective group id -effective group name... (3 Replies)
Discussion started by: Yifan_Guo
3 Replies

10. Shell Programming and Scripting

Running two processes in background

hi there, here's what i need in my korn-shell: ... begin korn-shell script ... nohup process_A.ksh ; nohup process_B.ksh & ... "other stuff" ... end lorn-shell script in plain english i want process A and process B to run in the background so that the script can continue doing... (6 Replies)
Discussion started by: jacob_gs
6 Replies

Featured Tech Videos