Предисловие br Началось все просто банально mpd мой любимец попал под

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<i>Предисловие:</i>
<br>Началось все просто и банально: <b>mpd</b>, мой любимец, попал под подозрение в деле о необъяснимо крупном хищении системных ресурсов. Но он был хитрый и не жрал постоянно и много. Погуглив, нашел несколько решений, связанных большей частью с <i>audio_format</i> в секции вывода. Но присмотревшись к системному монитору в <i>conky</i>, стало ясно, что не все гладко&#8230;
<br>
<br>Сидеть и смотреть на монитор часами, чтобы понять, действует решение или нет, точно не было желания и времени. Что и натолкнуло на мысль мониторить процесс.
<br>Конечно, сделать небольшой лог вполне бы хватало, но хотелось больше и нагляднее. Хотелось так:
<br>
<br><div align="center"><img src="http://pic.ipicture.ru/uploads/090719/qsmnOUVoKo.png" border="0"></div>
<br><!--more-->Ну и во многом благодаря <a href="http://mydebianblog.blogspot.com">&#8220;Запискам дебианщика&#8221;</a> о <b>gnuplot</b>, я накидал следущее:
<br><br><b><u>watcher.sh</u></b>
<br><br>
<div class="highlight"><pre><span style="color: #408080; font-style: italic">#!/bin/bash</span>
<span style="color: #408080; font-style: italic">#</span>
<span style="color: #408080; font-style: italic"># Written by razum2um</span>
<span style="color: #408080; font-style: italic"># Last modified Sun, 20 Jul 2009 00:43:29 +0700</span>
<span style="color: #408080; font-style: italic">#</span>
<span style="color: #408080; font-style: italic">###################################################################################</span>
<span style="color: #408080; font-style: italic">################################# <span class="caps">USER</span>-<span class="caps">DENINED</span> ####################################</span>
<span style="color: #408080; font-style: italic">###################################################################################</span>
<span style="color: #19177C"><span class="caps">TIMEZONE</span></span><span style="color: #666666">=</span>7 <span style="color: #408080; font-style: italic"># Your timezone ( <span class="caps">GMT</span>+ )</span>
<span style="color: #19177C"><span class="caps">DELAY</span></span><span style="color: #666666">=</span>1 <span style="color: #408080; font-style: italic"># pause between logging (sec)</span>
<span style="color: #19177C">DELTA_END_M</span><span style="color: #666666">=</span>60 <span style="color: #408080; font-style: italic"># monitoring time ( min, default 1h )</span>
<span style="color: #19177C">OUTPUT_FILE_PREFIX</span><span style="color: #666666">=</span>watched_for
<span style="color: #19177C">OUTPUT_FILE_DIR</span><span style="color: #666666">=</span>~
<span style="color: #19177C">TMP_FILE</span><span style="color: #666666">=</span>~/.watcher <span style="color: #408080; font-style: italic"># log-file</span>
<span style="color: #408080; font-style: italic">###################################################################################</span>
<span style="color: #408080; font-style: italic">############################## <span class="caps">PARAMETERS</span> <span class="caps">CHECK</span> ###################################</span>
<span style="color: #408080; font-style: italic">###################################################################################</span>
<span style="color: #19177C">USAGE_STRING</span><span style="color: #666666">=</span><span style="color: #BA2121">&quot;Usage: $0 &lt;process name&gt; [monitoring time (min) [pause between logging (sec)]]&quot;</span>
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #666666">[</span> <span style="color: #19177C">$# </span>-eq 3 <span style="color: #666666">]</span>
<span style="color: #008000; font-weight: bold">then</span>
<span style="color: #008000; font-weight: bold"> </span><span style="color: #19177C"><span class="caps">DELAY</span></span><span style="color: #666666">=</span><span style="color: #19177C">$3</span>
<span style="color: #19177C">DELTA_END_M</span><span style="color: #666666">=</span><span style="color: #19177C">$2</span>
<span style="color: #008000; font-weight: bold">elif</span> <span style="color: #666666">[</span> <span style="color: #19177C">$# </span>-eq 2 <span style="color: #666666">]</span>
<span style="color: #008000; font-weight: bold">then</span>
<span style="color: #008000; font-weight: bold"> </span><span style="color: #19177C">DELTA_END_M</span><span style="color: #666666">=</span><span style="color: #19177C">$2</span>
<span style="color: #008000; font-weight: bold">elif</span> <span style="color: #666666">[</span> <span style="color: #19177C">$# </span>-eq 1 <span style="color: #666666">]</span>
<span style="color: #008000; font-weight: bold">then</span>
<span style="color: #008000; font-weight: bold"> </span><span style="color: #19177C">PID_PROC</span><span style="color: #666666">=</span><span style="color: #BA2121">`</span>pidof <span style="color: #19177C">$1</span> | head -n1<span style="color: #BA2121">`</span>
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #666666">[</span> <span style="color: #BA2121">`</span>top -b -n 1 -p <span style="color: #19177C">$PID_PROC</span> | grep <span style="color: #19177C">$1</span> | wc -l<span style="color: #BA2121">`</span> -eq 0 <span style="color: #666666">]</span>
<span style="color: #008000; font-weight: bold">then</span>
<span style="color: #008000; font-weight: bold"> </span><span style="color: #008000">echo</span> <span style="color: #BA2121">&quot;Process $1 doesn&#39;t exist!&quot;</span>
<span style="color: #008000">exit </span>1
<span style="color: #008000; font-weight: bold">fi</span>
<span style="color: #008000; font-weight: bold">else</span>
<span style="color: #008000; font-weight: bold"> </span><span style="color: #008000">echo</span> <span style="color: #19177C">$USAGE_STRING</span>
<span style="color: #008000">exit </span>1
<span style="color: #008000; font-weight: bold">fi</span>
<span style="color: #19177C"><span class="caps">PROC</span></span><span style="color: #666666">=</span><span style="color: #19177C">$1</span>
<span style="color: #19177C">PID_PROC</span><span style="color: #666666">=</span><span style="color: #BA2121">`</span>pidof <span style="color: #19177C">$1</span> | head -n1<span style="color: #BA2121">`</span>
<span style="color: #19177C">START_TIME</span><span style="color: #666666">=</span><span style="color: #BA2121">`</span>date +%s<span style="color: #BA2121">`</span>
<span style="color: #19177C">DELTA_END_SEC</span><span style="color: #666666">=</span><span style="color: #BA2121">`</span>expr <span style="color: #19177C">$DELTA_END_M</span> <span style="color: #BB6622; font-weight: bold">\*</span> 60<span style="color: #BA2121">`</span>
<span style="color: #19177C">END_TIME</span><span style="color: #666666">=</span><span style="color: #BA2121">`</span>expr <span style="color: #19177C">$START_TIME</span> + <span style="color: #19177C">$DELTA_END_SEC</span><span style="color: #BA2121">`</span>
<span style="color: #19177C">TIME_SHIFT</span><span style="color: #666666">=</span><span style="color: #BA2121">`</span>expr <span style="color: #19177C">$<span class="caps">TIMEZONE</span></span> <span style="color: #BB6622; font-weight: bold">\*</span> 3600<span style="color: #BA2121">`</span> <span style="color: #408080; font-style: italic"># to be added to time written into log</span>
<span style="color: #19177C">OUTPUT_FILE_PATH</span><span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">${</span><span style="color: #19177C">OUTPUT_FILE_DIR</span><span style="color: #008000; font-weight: bold">}</span>/<span style="color: #008000; font-weight: bold">${</span><span style="color: #19177C">OUTPUT_FILE_PREFIX</span><span style="color: #008000; font-weight: bold">}</span>_<span style="color: #008000; font-weight: bold">${</span><span style="color: #19177C"><span class="caps">PROC</span></span><span style="color: #008000; font-weight: bold">}</span>.png
<span style="color: #408080; font-style: italic">###################################################################################</span>
<span style="color: #408080; font-style: italic">##################################### <span class="caps">INFO</span> #######################################</span>
<span style="color: #408080; font-style: italic">###################################################################################</span>
<span style="color: #19177C">FONT_BOLD</span><span style="color: #666666">=</span><span style="color: #BA2121">&#39;\e[1m&#39;</span>
<span style="color: #19177C">FONT_GREEN_BOLD</span><span style="color: #666666">=</span><span style="color: #BA2121">&#39;\e[32m&#39;</span><span style="color: #19177C">$FONT_BOLD</span>
<span style="color: #19177C">FONT_RED_BOLD</span><span style="color: #666666">=</span><span style="color: #BA2121">&#39;\e[31m&#39;</span><span style="color: #19177C">$FONT_BOLD</span>
<span style="color: #19177C">FONT_NORM</span><span style="color: #666666">=</span><span style="color: #BA2121">&#39;\e[0m&#39;</span>
<span style="color: #008000">echo</span> -e <span style="color: #BA2121">&quot;Now\t\t\t $FONT_GREEN_BOLD`date +%c`$FONT_NORM&quot;</span>
<span style="color: #008000">echo</span> -e <span style="color: #BA2121">&quot;Stops at\t\t $FONT_RED_BOLD`date -d @$END_TIME +%c`$FONT_NORM&quot;</span>
<span style="color: #008000">echo</span>
<span style="color: #008000">echo</span> -e <span style="color: #BA2121">&quot;${FONT_BOLD}Output into$FONT_NORM\t\t $OUTPUT_FILE_PATH&quot;</span>
<span style="color: #008000">echo</span> -e <span style="color: #BA2121">&quot;${FONT_BOLD}Log into$FONT_NORM\t\t $TMP_FILE&quot;</span>
<span style="color: #408080; font-style: italic">###################################################################################</span>
<span style="color: #408080; font-style: italic">################################### <span class="caps">LOGGING</span> #######################################</span>
<span style="color: #408080; font-style: italic">###################################################################################</span>
<span style="color: #008000">echo</span> -n &gt; <span style="color: #19177C">$TMP_FILE</span>
<span style="color: #008000; font-weight: bold">while</span> <span style="color: #666666">[</span> <span style="color: #BA2121">`</span>date +%s<span style="color: #BA2121">`</span> -lt <span style="color: #19177C">$END_TIME</span> <span style="color: #666666">]</span>
<span style="color: #008000; font-weight: bold">do</span>
<span style="color: #008000; font-weight: bold"> </span><span style="color: #19177C"><span class="caps">NOW</span></span><span style="color: #666666">=</span><span style="color: #BA2121">`</span>date +%s<span style="color: #BA2121">`</span>
<span style="color: #008000">echo</span> -n <span style="color: #BA2121">&quot;`expr $<span class="caps">NOW</span> + $TIME_SHIFT` &quot;</span> &gt;&gt; <span style="color: #19177C">$TMP_FILE</span>
top -b -n 1 -p <span style="color: #19177C">$PID_PROC</span> | grep <span style="color: #19177C">$<span class="caps">PROC</span></span> &gt;&gt; <span style="color: #19177C">$TMP_FILE</span>
sleep <span style="color: #19177C">$<span class="caps">DELAY</span></span>
<span style="color: #008000; font-weight: bold">done</span>
<span style="color: #408080; font-style: italic">###################################################################################</span>
<span style="color: #408080; font-style: italic">################################### <span class="caps">PLOTTING</span> ######################################</span>
<span style="color: #408080; font-style: italic">###################################################################################</span>
<span style="color: #19177C">PERFORMED_AT</span><span style="color: #666666">=</span><span style="color: #BA2121">`</span>date +%d/%m/%y<span style="color: #BA2121">`</span>
<span style="color: #19177C">U_MAX_LOAD_TIME</span><span style="color: #666666">=</span><span style="color: #BA2121">`</span>cat .watcher | awk <span style="color: #BA2121">&#39;{print $10 &quot; &quot; $1}&#39;</span> | sort | tail -n1 | awk <span style="color: #BA2121">&#39;{print $2}&#39;`</span>
<span style="color: #19177C">U_MIN_LOAD_TIME</span><span style="color: #666666">=</span><span style="color: #BA2121">`</span>cat .watcher | awk <span style="color: #BA2121">&#39;{print $10 &quot; &quot; $1}&#39;</span> | sort -r | tail -n1 | awk <span style="color: #BA2121">&#39;{print $2}&#39;`</span>
<span style="color: #19177C">REAL_U_MAX_LOAD_TIME</span><span style="color: #666666">=</span><span style="color: #BA2121">`</span>expr <span style="color: #19177C">$U_MAX_LOAD_TIME</span> &mdash; <span style="color: #19177C">$TIME_SHIFT</span><span style="color: #BA2121">`</span>
<span style="color: #19177C">REAL_U_MIN_LOAD_TIME</span><span style="color: #666666">=</span><span style="color: #BA2121">`</span>expr <span style="color: #19177C">$U_MIN_LOAD_TIME</span> &mdash; <span style="color: #19177C">$TIME_SHIFT</span><span style="color: #BA2121">`</span>
<span style="color: #19177C">REAL_MAX_LOAD_TIME</span><span style="color: #666666">=</span><span style="color: #BA2121">`</span>date -d @<span style="color: #19177C">$REAL_U_MAX_LOAD_TIME</span> +%H:%M:%S<span style="color: #BA2121">`</span>
<span style="color: #19177C">REAL_MIN_LOAD_TIME</span><span style="color: #666666">=</span><span style="color: #BA2121">`</span>date -d @<span style="color: #19177C">$REAL_U_MIN_LOAD_TIME</span> +%H:%M:%S<span style="color: #BA2121">`</span>
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #666666">[</span> <span style="color: #19177C">$DELTA_END_M</span> -lt 60 <span style="color: #666666">]</span>
<span style="color: #008000; font-weight: bold">then</span>
<span style="color: #008000; font-weight: bold"> </span><span style="color: #19177C">FORMAT_X</span><span style="color: #666666">=</span><span style="color: #BA2121">&#39;%Mm%Ss&#39;</span>
<span style="color: #008000; font-weight: bold">else</span>
<span style="color: #008000; font-weight: bold"> </span><span style="color: #19177C">FORMAT_X</span><span style="color: #666666">=</span><span style="color: #BA2121">&#39;%Hh%Mm&#39;</span>
<span style="color: #008000; font-weight: bold">fi</span>
<span style="color: #008000">echo</span> <span style="color: #BA2121">&quot;set terminal png enhanced </span>
<span style="color: #BA2121">set output \&quot;$OUTPUT_FILE_PATH\&quot;</span>
<span style="color: #BA2121">set xdata time</span>
<span style="color: #BA2121">set timefmt \&quot;%s\&quot;</span>
<span style="color: #BA2121">set format x \&quot;$FORMAT_X\&quot;</span>
<span style="color: #BA2121">set xlabel \&quot;Time\&quot;</span>
<span style="color: #BA2121">set ylabel \&quot;<span class="caps">CPU</span> load, %\&quot;</span>
<span style="color: #BA2121">set yrange [0:100]</span>
<span style="color: #BA2121">set title \&quot;$<span class="caps">PROC</span> at $PERFORMED_AT \n Maximum at: $REAL_MAX_LOAD_TIME \n Minimum at: $REAL_MIN_LOAD_TIME\&quot;</span>
<span style="color: #BA2121">plot \&quot;~/.watcher\&quot; using 1:10 notitle with lines&quot;</span> | gnuplot
<span style="color: #408080; font-style: italic">###################################################################################</span>
rm <span style="color: #19177C">$TMP_FILE</span>
<span style="color: #008000">echo</span> <span style="color: #BA2121">&quot;$OUTPUT_FILE_PATH created successfully&quot;</span>
<span style="color: #008000">exit </span>0
</pre></div>
<br><br><b></b>Не удержался и сделал на выходе отдельные &#8220;цветные рюшечки&#8221;, памятуя о <a href="http://razum2um.alwaysdata.net/2009/07/09/glamurnaya-konsol/">этом посте</a>:
<br>
<br><div align="center"><img src="http://pic.ipicture.ru/uploads/090719/mx69wfHuS2.png" border="0"></div>
<br>Работает просто:
<br><br><code><b>./watcher.sh ИМЯ_ПРОЦЕССА [ДЛИТЕЛЬНОСТЬ_НАБЛЮДЕНИЯ (мин) [ПЕРИОД_ЗАМЕРОВ (сек.)]]</b></code><br>
<br>
<br>Последние два &mdash; необязательны (по умолчанию замеры делаются каждую секунду на протяжении часа), т.е., например, можно и
<br><br><code><b>./watcher.sh mpd</b></code><br>
<br>
<br><i>И не забудьте поправить значение <span class="caps">TIMEZONE</span>, иначе на графике будет сдвиг по оси времени!</i>
<br>
<br><span class="caps">PS</span>
<br>На графике показана, конечно же, тестовая нагрузка:
<br><code>for i in `seq 1 300` ;do mpc next 1>/dev/null; done</code>