1. Some times the Thread counts monitoring cannot help to know the status of the Server instance then need to look for the instance is alive or not. That is the java process exist for that particular instance.
2. You may get Stuck Thread alert for any machine through your monitoring tools (HP OVO, Introscope or some other). Then you need to get the thread dump for the stuck thread found WebLogic instance.
3. You may want to kill an instance for any abnormal reasons
What is 'ps' command does?
The 'ps' command gives us information about processes status on Solaris or Linux or any other UNIX flavor. There are different varieties of 'ps' command pathsi) /usr/bin/ps
ii) /usr/ucb/ps
If you type “ps” at the prompt, and you get very little information about the process.
ps command switches
The ps command switches are key, as always required to get the desired output of ps command. To get a complete list, for instance, type switches with prefix - symbol.- “ps -A” (Note: Linux is case-sensitive). Also,
- “ps -e” will give you a complete list of processes running on the machine.
- "ps -f" give a full listing, i.e. more information about each process listed
- "ps -u username" will list all processes running by a specific user mentioned as argument value
- "ps -l" gives a long listing of processes. This is even long data than in a full listing
The power to this 'ps' command will be filled up with its options, If you know how to use the options you can make wonders!! Now I need to know what are those options which could help me to write the script. You tell me what to do? Well your answer is go to our super duper search engine 'Google'. ps(1B) man page on Oracle sun site. I selected -a, -ww, -x options that could help me to get complete command details of a process id associated with it.
Now I need to use 'cut' command for the WebLogic instance name from the big process detailed text. This is really challenge dear, what you suggest dear super reader? After running between many sites and man pages I got to know few ideas on awk and its family nawk command script.
This script is a generic script I had executed for WebLogic 9.2, 11g also. You can apply this for WebLgoic 7/8.1 also.
# FileName: wlsPid.sh # This script will Fetches PID of WebLogic Server instances # ========================================================= clear echo "PID associated with WebLogic instances" echo "=====================================" /usr/ucb/ps -awwx | grep "weblogic.Name" | grep -v "grep weblogic.Name" | nawk 'BEGIN {print "PID\tWLServer"; print "=====================================" } ; { NUM = match($0, "weblogic.Name=") ; START_POS = RSTART+RLENGTH ; START_STR = substr($0, START_POS) ; FINISH = match(START_STR, " ") ; FINISH_POS = START_POS+RSTART+RLENGTH ; FINISH_STR = substr($0, START_POS, FINISH_POS) ; NUM = split(FINISH_STR,FINISH_ARRAY) ; printf ("%s\t%s\n",$1, FINISH_ARRAY[1]) ; } END { print "===================================="}' #=== Published on http://wlatricksntips.blogspot.com =====
The above script is executed on Solaris 9, 10 the output will look like as follows:
PID associated with WebLogic instances ===================================== PID WLServer =====================================
1354 admin 11469 mserver01 11581 mserver02
Similar type of script is required for Linux machines too. Then I need to change the above script in 3 places.
The ps command options, Linux supports gawk (GNU awk) instead of nawk. The final thing to change is the string that requires not to grep useless string.
Perl for WebLogic on Linux
With the ps command we can get the desired output onto standard output. Perl having great Match, split built-in functions. In Linkedin Perl group, I followed Ashutosh Kukreti tips. You must be member so that you can see the discussion. I had posted the question about my requirement. After looking into Perl regular expressions usage.
#!/usr/bin/perl $plist = `ps -U \$LOGNAME -o pid,pcpu,size,cmd|grep -i weblogic.Name|egrep -v grep`; $CPU_THRESHOLD=10; @prlist = split(/\n/ ,$plist ); print "PID SERVER CPU SIZE DOMAIN DIR ------ --------------- ------ ------ ----------------------------------- "; $i=0; foreach $_ (@prlist){ $x=$prlist[$i]; $x=~ m/^\s*(\d+)\s.+\s+-Dweblogic\.Name=(\S+)\s+/ ||next; my @values = split(' ',$x); $pid=$1; $server=$2; $cpu=$values[1]; if($cpu > $CPU_THRESHOLD) { print "\n\nSUGGESTED ACTION:\n" . "-----------------\n" . "Use this command to trace threads on servers " . "with over $CPU_THRESHOLD% CPU usage:\n" . "/usr/bin/kill -3 " . $pid."\n"; } $mem=int($values[2]/1024); $r = `/usr/bin/pwdx $pid 2>&1`; $r =~ s/^\d+://; $r =~ s/^\s+|\s$//g; print $pid, "\t", $server,"\t",$cpu,"\t",$mem,"M\t",$r,"\n"; $i++; }
This part of Perl script, my friend SRIKANTH PANDA helped me to get process id and its related info with ps command.
WebLogic PID, CPU Load, MEM Automation script - Perl Script |
Here in this script you can set your application desired CPU_THRESHOLD value to get notifying mails. After saving the file provide the execution permissions with chmod u+X wlsps.pl. The following is the output which I've executed on Ubuntu Linux with WebLogic 12c.
$ ./wlsps.pl PID SERVER CPU SIZE DOMAIN DIR ------ --------------- ------ ------ ----------------------------------- 2518 AdminServer 2.8 860M /home/pavanwla/wlsdomains/medrec
Enjoy the fun with scripting tricks on your latest WebLogic environments, keep posting your updates and thoughts on this blog. Share this blog in your technical blogs.