在进程监控脚本中,我们通常需要根据脚本的参数来确定有哪些性能参数将被收集,当这些性能参数大于最高阈值或小于最低阈值时,监控脚本将根据实际的情况,采取预置的措施,如邮件通知、直接杀死进程等,这里我们给出的例子是收集进程运行时长性能参数。
ps命令的etime值将给出每个进程的运行时长,其格式主要为以下三种:
1. minutes:seconds,如20:30
2. hours:minutes:seconds,如1:20:30
3. days-hours:minute:seconds,如2-18:20:30
该脚本将会同时处理这三种格式的时间信息,并最终转换为进程所流经的分钟数。
[root@xieqichao ~]
pid_string=`ps -eo pid,etime,comm | grep "init" | grep -v grep`
exec_time=`echo $pid_string | awk '{print $2}'`
time_field_count=`echo $exec_time | awk -F: '{print NF}'`
count_of_minutes=`echo $exec_time | awk -F: '{print $(NF-1)}'`
<span class="token comment">#6. 判断当前exec_time变量存储的时间数据是属于以上哪种格式。</span>
<span class="token comment">#7. 如果是第一种,那么天数和小时数均为0。</span>
<span class="token comment">#8. 如果是后两种之一,则需要继续判断到底是第一种还是第二种,如果是第二种,其小时部分将不存在横线(-)分隔符分隔天数和小时数,否则需要将这两个时间字段继续拆分,以获取具体的天数和小时数。对于第二种,天数为0.</span>
<span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token variable">$time_field_count</span> -lt 3 <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
count_of_hours<span class="token operator">=</span>0
count_of_days<span class="token operator">=</span>0
<span class="token keyword">else</span>
count_of_hours<span class="token operator">=</span><span class="token variable"><span class="token variable">`</span><span class="token keyword">echo</span> $exec_time <span class="token operator">|</span> <span class="token function">awk</span> -F: <span class="token string">'{print <span class="token variable"><span class="token variable">$(</span>NF-2<span class="token variable">)</span></span>}'</span><span class="token variable">`</span></span>
fields<span class="token operator">=</span><span class="token variable"><span class="token variable">`</span><span class="token keyword">echo</span> $count_of_hours <span class="token operator">|</span> <span class="token function">awk</span> -F- <span class="token string">'{print NF}'</span><span class="token variable">`</span></span>
<span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token variable">$fields</span> -ne 1 <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
count_of_days<span class="token operator">=</span><span class="token variable"><span class="token variable">`</span><span class="token keyword">echo</span> $count_of_hours <span class="token operator">|</span> <span class="token function">awk</span> -F- <span class="token string">'{print <span class="token variable">$1</span>}'</span><span class="token variable">`</span></span>
count_of_hours<span class="token operator">=</span><span class="token variable"><span class="token variable">`</span><span class="token keyword">echo</span> $count_of_hours <span class="token operator">|</span> <span class="token function">awk</span> -F- <span class="token string">'{print <span class="token variable">$2</span>}'</span><span class="token variable">`</span></span>
<span class="token keyword">else</span>
count_of_days<span class="token operator">=</span>0
<span class="token keyword">fi</span>
<span class="token keyword">fi</span>
<span class="token comment">#9. 通过之前代码获取的各个字段值,计算出该进程实际所流经的分钟数。</span>
<span class="token comment">#10. bc命令是计算器命令,可以将echo输出的数学表达式计算为最终的数字值。</span>
elapsed_minutes<span class="token operator">=</span><span class="token variable"><span class="token variable">`</span><span class="token keyword">echo</span> <span class="token string">"<span class="token variable">$count_of_days*1440</span>+<span class="token variable">$count_of_hours*60</span>+<span class="token variable">$count_of_minutes</span>"</span> <span class="token operator">|</span> <span class="token function">bc</span><span class="token variable">`</span></span>
<span class="token keyword">echo</span> <span class="token string">"The elapsed minutes of init process is"</span> <span class="token variable">$elapsed_minutes</span> <span class="token string">"minutes."</span>
CTRL+D
<span class="token punctuation">[</span>root@xieqichao ~<span class="token punctuation">]</span><span class="token comment"># ./test11.sh</span>
The elapsed minutes of init process is 577 minutes.