cgi_memo 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050
  1. #!/bin/sh
  2. # $Id: cgi_memo,v 1.99 2022/09/15 08:25:31 gilles Exp gilles $
  3. if test -n "$1"; then
  4. echoq() { echo "$@" ; } # not quiet mode
  5. else
  6. echoq() { : ; } # quiet mode: nop
  7. fi
  8. run_test() {
  9. tests_count=`expr 1 + $tests_count`
  10. # do not run anything between the two following instructions
  11. "$@"; run_test_status=$?
  12. # now you can run something since $? is saved
  13. if test x"$run_test_status" = x"0"; then
  14. echo "ok $tests_count $@"
  15. else
  16. echo "not ok $tests_count $@"
  17. tests_failed_count=`expr 1 + $tests_failed_count`
  18. tests_failed_list="$tests_failed_list $tests_count"
  19. fi
  20. return $run_test_status
  21. }
  22. run_tests() {
  23. tests_count=0
  24. tests_failed_count=0
  25. tests_failed_list=
  26. for t in "$@"; do
  27. echo "### running $t"
  28. "$t"
  29. done
  30. echo
  31. echo "#### ALL tests done"
  32. if test 0 -eq $tests_failed_count; then
  33. echo "ALL $tests_count TESTS SUCCESSFUL"
  34. return 0
  35. else
  36. # At least one failed
  37. echo "FAILED $tests_failed_count/$tests_count TESTS: $tests_failed_list"
  38. return 1
  39. fi
  40. }
  41. tests()
  42. {
  43. :
  44. # All tests
  45. run_tests \
  46. tests_pattern_filename \
  47. tests_grep_last_modified \
  48. tests_grep_date \
  49. tests_date_to_epoch \
  50. tests_diff_current_vs_last_modified
  51. }
  52. tests_all_verbose_if_failure()
  53. {
  54. # Run tests silent but if failure then rerun them verbose.
  55. # return 0 if all tests passed
  56. # return 1 if some tests failed
  57. if ! tests > /dev/null 3>&1 ; then
  58. tests
  59. return 1
  60. fi
  61. return 0
  62. }
  63. #### Variable definitions
  64. tests_count=0
  65. tests_failed_count=0
  66. here_is_freebsd()
  67. {
  68. test FreeBSD = `uname -s`
  69. }
  70. here_is_linux()
  71. {
  72. test Linux = `uname -s`
  73. }
  74. ram_total_in_bytes()
  75. {
  76. here_is_linux && grep MemTotal /proc/meminfo | awk '{print $2 " * 1024"}' | bc && return
  77. here_is_freebsd && sysctl -n hw.physmem && return
  78. echo 1
  79. }
  80. ram_total_in_KiB()
  81. {
  82. here_is_linux && grep MemTotal /proc/meminfo | awk '{print $2}' && return
  83. here_is_freebsd && sysctl -n hw.physmem | awk '{print $1 " / 1024"}' | bc && return
  84. echo 1
  85. }
  86. ram_total_var()
  87. {
  88. ram_total_in_bytes=`ram_total_in_bytes`
  89. ram_total_in_KiB=`ram_total_in_KiB`
  90. ram_total_human=`bytestohuman $ram_total_in_bytes`
  91. }
  92. ram_total_var
  93. echoq ram_total
  94. ram_total()
  95. {
  96. echo ram_total_in_KiB=$ram_total_in_KiB
  97. echo ram_total_in_bytes=$ram_total_in_bytes
  98. echo ram_total_human=$ram_total_human
  99. }
  100. echoq list_all_logs
  101. list_all_logs()
  102. {
  103. cat list_all_logs_auto.txt | grep -a -v 385d7a4d8d428d7aa2b57c8982629e2bd67698ed/ | grep -a "$1"
  104. }
  105. echoq list_all_logs_generate
  106. list_all_logs_generate()
  107. {
  108. echo Result in list_all_logs.txt
  109. sortmtimef . | grep -a -v perl.core.txt | grep -a -v 385d7a4d8d428d7aa2b57c8982629e2bd67698ed/ | grep -a \./......................................../ | grep -a \.txt > list_all_logs.txt.tmp
  110. mv list_all_logs.txt.tmp list_all_logs.txt
  111. }
  112. list_log_matching()
  113. {
  114. pattern="$1"
  115. # Ignore no args runs going to 385d7a4d8d428d7aa2b57c8982629e2bd67698ed
  116. cat list_all_logs_auto.txt | grep -a -v 385d7a4d8d428d7aa2b57c8982629e2bd67698ed | egrep -a -- "$pattern"
  117. }
  118. statsfile()
  119. {
  120. base=`pattern_filename G $1 $2`
  121. statsfile=$base.txt
  122. echo $statsfile
  123. }
  124. echoq biggest_transfer
  125. biggest_transfer()
  126. {
  127. statsfile=`statsfile Total_bytes_transferred $1`
  128. bytestohuman `datamash_file_op_index "$statsfile" max 5`
  129. }
  130. echoq total_bytes_transferred
  131. total_bytes_transferred()
  132. {
  133. statsfile=`statsfile Total_bytes_transferred $1`
  134. datamash_file_op_index "$statsfile" sum 5
  135. }
  136. # Total volume transferred
  137. echoq total_volume_transferred
  138. total_volume_transferred()
  139. {
  140. #echo -n 'numfmt --to=iec-i '
  141. bytestohuman `total_bytes_transferred $1`
  142. }
  143. echoq total_messages_transferred
  144. total_messages_transferred()
  145. {
  146. statsfile=`statsfile Messages_transferred $1`
  147. datamash_file_op_index "$statsfile" sum 4 %16.0f | tr -d ' '
  148. }
  149. longest_transfer()
  150. {
  151. statsfile=`statsfile Transfer_time $1`
  152. LC_ALL=C printf "%.0f\n" `datamash_file_op_index "$statsfile" max 4`
  153. }
  154. echoq pids_of_imapsync_running
  155. pids_of_imapsync_running() {
  156. pgrep -d ' ' -f cgi-bin/imapsync
  157. : # always return true
  158. }
  159. echoq number_of_imapsync_running
  160. number_of_imapsync_running()
  161. {
  162. pids_of_imapsync_running | wc -w | tr -d ' '
  163. : # always return true
  164. }
  165. echoq pids_of_proximapsync_running
  166. pids_of_proximapsync_running() {
  167. pgrep -d ' ' -f cgi-bin/proximapsync
  168. : # always return true
  169. }
  170. echoq number_of_proximapsync_running
  171. number_of_proximapsync_running()
  172. {
  173. pids_of_proximapsync_running | wc -w | tr -d ' '
  174. : # always return true
  175. }
  176. echoq memory_used_by_all_proximapsync_KiB
  177. memory_used_by_all_proximapsync_KiB()
  178. {
  179. # Sum up all memory taken by imapsync runs, in KiB.
  180. pids_of_proximapsync_running=`pids_of_proximapsync_running`
  181. if test -n "$pids_of_proximapsync_running" ; then
  182. ps -o rss -p $pids_of_proximapsync_running | sed 1,1d | datamash sum 1
  183. else
  184. echo 0
  185. fi
  186. }
  187. echoq number_and_pids_of_imapsync_running
  188. number_and_pids_of_imapsync_running()
  189. {
  190. echo "`number_of_imapsync_running` : `pids_of_imapsync_running`"
  191. : # always return true
  192. }
  193. echoq memory_used_by_all_imapsync_KiB
  194. memory_used_by_all_imapsync_KiB()
  195. {
  196. # Sum up all memory taken by imapsync runs, in KiB.
  197. pids_of_imapsync_running=`pids_of_imapsync_running`
  198. if test -n "$pids_of_imapsync_running" ; then
  199. ps -o rss -p $pids_of_imapsync_running | sed 1,1d | datamash sum 1
  200. else
  201. echo 0
  202. fi
  203. }
  204. echoq memory_used_by_all_processes_KiB
  205. memory_used_by_all_processes_KiB()
  206. {
  207. # Sum up all memory taken by all processes, in KiB.
  208. ps -o rss -p `pgrep -f '.*'` | sed 1,1d | datamash sum 1
  209. }
  210. memory_used_by_all_processes_var()
  211. {
  212. memory_used_by_all_processes_KiB=`memory_used_by_all_processes_KiB`
  213. memory_used_by_all_processes_bytes=`KiBytes_to_Bytes $memory_used_by_all_processes_KiB`
  214. # $ram_total_in_bytes is fixed so done at the beginning
  215. memory_used_by_all_processes_percent=`ratio_percent $memory_used_by_all_processes_bytes $ram_total_in_bytes`
  216. memory_used_by_all_processes_human=`bytestohuman $memory_used_by_all_processes_bytes`
  217. }
  218. echoq memory_used_by_all_processes
  219. memory_used_by_all_processes()
  220. {
  221. memory_used_by_all_processes_var
  222. echo memory_used_by_all_processes_KiB=$memory_used_by_all_processes_KiB
  223. echo memory_used_by_all_processes_bytes=$memory_used_by_all_processes_bytes
  224. echo memory_used_by_all_processes_percent=$memory_used_by_all_processes_percent
  225. echo memory_used_by_all_processes_human=$memory_used_by_all_processes_human
  226. }
  227. memory_available_var()
  228. {
  229. memory_used_by_all_processes_KiB=`memory_used_by_all_processes_KiB`
  230. memory_used_by_all_processes_bytes=`KiBytes_to_Bytes $memory_used_by_all_processes_KiB`
  231. memory_available_in_bytes=`expr $ram_total_in_bytes - $memory_used_by_all_processes_bytes`
  232. memory_available_human=`bytestohuman $memory_available_in_bytes`
  233. memory_available_in_slots=`expr $memory_available_in_bytes / 250000000`
  234. }
  235. memory_available()
  236. {
  237. memory_available_var
  238. echo memory_available_in_bytes=$memory_available_in_bytes
  239. echo memory_available_human=$memory_available_human
  240. echo memory_available_in_slots=$memory_available_in_slots
  241. }
  242. memory_available_in_slots()
  243. {
  244. memory_available_var
  245. echo $memory_available_in_slots
  246. }
  247. memory_used_by_all_imapsync_var()
  248. {
  249. memory_used_by_all_imapsync_KiB=`memory_used_by_all_imapsync_KiB`
  250. memory_used_by_all_imapsync_bytes=`KiBytes_to_Bytes $memory_used_by_all_imapsync_KiB`
  251. # $ram_total_in_bytes is fixed so done at the beginning
  252. memory_used_by_all_imapsync_percent=`ratio_percent $memory_used_by_all_imapsync_bytes $ram_total_in_bytes`
  253. memory_used_by_all_imapsync_human=`bytestohuman $memory_used_by_all_imapsync_bytes`
  254. }
  255. echoq memory_used_by_all_imapsync
  256. memory_used_by_all_imapsync()
  257. {
  258. memory_used_by_all_imapsync_var
  259. echo memory_used_by_all_imapsync_KiB=$memory_used_by_all_imapsync_KiB
  260. echo memory_used_by_all_imapsync_bytes=$memory_used_by_all_imapsync_bytes
  261. echo memory_used_by_all_imapsync_percent=$memory_used_by_all_imapsync_percent
  262. echo memory_used_by_all_imapsync_human=$memory_used_by_all_imapsync_human
  263. }
  264. oom_immune_pid()
  265. {
  266. pidtoimmune=${1:-$$}
  267. # $2 is the immume value or -12
  268. test -f /proc/$pidtoimmune/oom_adj || return
  269. echo -n "$pidtoimmune "
  270. cat /proc/$pidtoimmune/oom_* | tr '\n' ' '
  271. { test -f /proc/$pidtoimmune/oom_adj && echo ${2:-"-12"} > /proc/$pidtoimmune/oom_adj && echo -n ">>> " && cat /proc/$pidtoimmune/oom_adj ; }
  272. }
  273. echoq oom_immune_imapsync_running
  274. oom_immune_imapsync_running() {
  275. for pidsimapsync in `pids_of_imapsync_running`
  276. do
  277. oom_immune_pid $pidsimapsync $1
  278. done
  279. }
  280. pidapache()
  281. {
  282. test -f /var/run/apache2/apache2.pid && cat /var/run/apache2/apache2.pid && return
  283. test -f /var/run/httpd/httpd.pid && cat /var/run/httpd/httpd.pid && return
  284. }
  285. echoq oom_immune_apache
  286. oom_immune_apache() {
  287. pidapache=`pidapache`
  288. echo "[$pidapache]"
  289. test -n "$pidapache" && oom_immune_pid "$pidapache" "$1" || echo no apache running
  290. }
  291. apache_status_restart()
  292. {
  293. echo 'systemctl --no-pager status apache2 ; systemctl --no-pager restart apache2 ; systemctl --no-pager status apache2'
  294. echo 'systemctl --no-pager status httpd ; systemctl --no-pager restart httpd ; systemctl --no-pager status httpd'
  295. }
  296. echoq nb_migrations_launched
  297. nb_migrations_launched() {
  298. list_all_logs | egrep "$1" | egrep -o [a-f0-9]{40} | sort | uniq -c | wc -l
  299. }
  300. nb_migrations_launched_old() {
  301. /bin/ls . | egrep [a-f0-9]{40} | sort | uniq | wc -l
  302. }
  303. echoq current_stats
  304. current_stats() {
  305. echo -n "Nb accounts: "; nb_migrations_launched
  306. echo -n "Nb imapsync running: "; number_and_pids_of_imapsync_running
  307. # dstat, Linux
  308. dstat --version > /dev/null 2>&1 && dstat -l -n -cdgyms 60 1 && return
  309. # no dstat, FreeBSD
  310. dstat --version > /dev/null 2>&1 || vmstat 2 15 && return
  311. #clear
  312. }
  313. echoq watch_current_stats
  314. watch_current_stats() {
  315. export -f current_stats
  316. # watch -n 120 current_stats
  317. while : ; do
  318. clear
  319. oom_immune_imapsync_running
  320. current_stats
  321. sleep 6
  322. done
  323. }
  324. echoq 'grep_in_all_logs str1 str2 ... # up to str5. Results in mtime order of logfiles'
  325. grep_in_all_logs() {
  326. grep_file=grep_`echo "$1 $2 $3 $4 $5" | tr ' ' '_' | tr -cd '0-9a-zA-Z_.\n'`.txt
  327. echo results in "${grep_file}"
  328. list_all_logs | tr '\n' '\000'| xargs -0 egrep -E -i "$1" | egrep -i "$2" | egrep -i "$3" | egrep -i "$4" | egrep -i "$5" | tee "${grep_file}.tmp"
  329. mv "${grep_file}.tmp" "${grep_file}"
  330. }
  331. echoq grep_in_logs_manual
  332. grep_in_logs_manual()
  333. {
  334. cat << EOF
  335. list_all_logs /2021_11 | tail -666 | tr '\n' '\000'| xargs -0 egrep -i LALALA | tee grep_LALALA.txt
  336. EOF
  337. }
  338. echoq 'grep_stats_from_list_all_logs # long'
  339. grep_stats_from_list_all_logs() {
  340. echo results in grep_stats.txt
  341. # remove empty lines because it would grep all lines of all logs
  342. sed -i".bak" '/^[[:space:]]*$/d' stat_patterns.txt
  343. list_all_logs | tr '\n' '\000'| xargs -0 egrep -i -f stat_patterns.txt > grep_stats.txt.tmp
  344. mv grep_stats.txt.tmp grep_stats.txt
  345. }
  346. tests_pattern_filename()
  347. {
  348. run_test test "" = "`pattern_filename`"
  349. run_test test "abcd" = "`pattern_filename abcd`"
  350. run_test test "ab_0123__4567_cd" = "`pattern_filename ab[0123][4567]cd`"
  351. run_test test "ab_cd" = "`pattern_filename ab cd`"
  352. run_test test "ab_cd" = "`pattern_filename ab cd `"
  353. run_test test "abcd" = "`pattern_filename ab\&cd`"
  354. run_test test "abcd" = "`pattern_filename ab""cd`"
  355. run_test test "abcd" = "`pattern_filename ab""cd`"
  356. run_test test "ab_cd" = "`pattern_filename ab" "cd`"
  357. run_test test "ab__cd" = "`pattern_filename ab" "cd`"
  358. run_test test "ab__cd" = "`pattern_filename ab "" cd`"
  359. run_test test "ab___cd" = "`pattern_filename ab " " cd`"
  360. run_test test "ab____cd" = "`pattern_filename ab " " cd`"
  361. run_test test "ab____cd" = "`pattern_filename ab " " cd`"
  362. run_test test "ab_____cd" = "`pattern_filename ab " " cd`"
  363. run_test test "a_b_c_d" = "`pattern_filename a b c d`"
  364. }
  365. pattern_filename()
  366. {
  367. echo "$@" | tr ' .[]' '____' | tr -cd '0-9a-zA-Z_.'
  368. }
  369. echoq 'grep_stats_from_list_log_matching lognamepattern # time depending on lognamepattern'
  370. grep_stats_from_list_log_matching() {
  371. pattern="$1"
  372. pattern_filename=`pattern_filename "$pattern"`
  373. results_filename=grep_stats_"$pattern_filename".txt
  374. echo results in "$results_filename"
  375. # remove empty lines because it would grep all lines of all logs
  376. sed -i".bak" '/^[[:space:]]*$/d' stat_patterns.txt
  377. list_log_matching "$pattern" | tr '\n' '\000'| xargs -0 egrep -i -f stat_patterns.txt > "$results_filename".tmp
  378. mv "$results_filename".tmp "$results_filename"
  379. }
  380. grep_any() {
  381. file=`statsfile "$1" "$2"`
  382. pattern_filtered=`pattern_filename "$2"`
  383. echo $file
  384. if test -f grep_stats_"$pattern_filtered".txt ; then
  385. egrep -i "$1" grep_stats_"$pattern_filtered".txt > $file.tmp
  386. mv $file.tmp $file
  387. else
  388. echo File not found: grep_stats_"$pattern_filtered".txt
  389. fi
  390. }
  391. grep_load() {
  392. file=`statsfile "Load" "$1"`
  393. pattern_filtered=`pattern_filename "$1"`
  394. echo $file
  395. if test -f grep_stats_"$pattern_filtered".txt ; then
  396. egrep -o 'Load is ..?\... ..?\... ..?\... .*' grep_stats_"$pattern_filtered".txt > $file.tmp
  397. mv $file.tmp $file
  398. else
  399. echo File not found: grep_stats_"$pattern_filtered".txt
  400. fi
  401. }
  402. stat_patterns_list() {
  403. cat stat_patterns.txt | sed '/^[[:space:]]*$/d' | tr -d '^'
  404. }
  405. echoq 'grep_all_stat_from_patterns_list lognamepattern # long'
  406. grep_all_stat_from_patterns_list() {
  407. grep_load "$1"
  408. stat_patterns_list | while read k; do grep_any "$k" "$1"; done
  409. }
  410. sum_first_column_G_HTTP_USER_AGENT_sorted()
  411. {
  412. awk '{sum += $1} END {print sum}' G_HTTP_USER_AGENT_${1}_sorted.txt
  413. }
  414. stat_useragent_X()
  415. {
  416. grep -o 'HTTP_USER_AGENT.*' G_HTTP_USER_AGENT_$1.txt \
  417. | tail -10000000 | sort | egrep -o -w 'Mozilla/5.0 \([^;]+' \
  418. | sort | egrep -o '\([a-zA-Z]+' | sort | uniq -c | sort -g \
  419. | grep -v KHTML | tr -d '(' > G_HTTP_USER_AGENT_${1}_sorted.txt
  420. }
  421. echoq 'percent_stat_useragent_X'
  422. percent_stat_useragent_X()
  423. {
  424. stat_useragent_X "$1"
  425. sum_first_column_G_HTTP_USER_AGENT=`sum_first_column_G_HTTP_USER_AGENT_sorted $1`
  426. { while read num_useragent useragent ; do
  427. #echo KK $num_useragent $useragent
  428. PerCent=`echo "scale=2; 100*$num_useragent/$sum_first_column_G_HTTP_USER_AGENT" | bc -l`
  429. echo "$useragent $PerCent % ( $num_useragent / $sum_first_column_G_HTTP_USER_AGENT )"
  430. done
  431. } < G_HTTP_USER_AGENT_${1}_sorted.txt
  432. }
  433. stat_load()
  434. {
  435. echo G_Load_$1.txt
  436. echo -n 'Load 1 min 5 min 15 min ' ; grep -o 'on.*cores' G_Load_$1.txt |sort| uniq
  437. echo -n 'Load min: ' ; LC_NUMERIC=C datamash --format=%3.1f -W min 3 min 4 min 5 < G_Load_$1.txt
  438. echo -n 'Load q1: ' ; LC_NUMERIC=C datamash --format=%3.1f -W q1 3 q1 4 q1 5 < G_Load_$1.txt
  439. echo -n 'Load median: ' ; LC_NUMERIC=C datamash --format=%3.1f -W median 3 median 4 median 5 < G_Load_$1.txt
  440. echo -n 'Load mean: ' ; LC_NUMERIC=C datamash --format=%3.1f -W mean 3 mean 4 mean 5 < G_Load_$1.txt
  441. echo -n 'Load q3: ' ; LC_NUMERIC=C datamash --format=%3.1f -W q3 3 q3 4 q3 5 < G_Load_$1.txt
  442. echo -n 'Load max: ' ; LC_NUMERIC=C datamash --format=%3.1f -W max 3 max 4 max 5 < G_Load_$1.txt
  443. }
  444. echoq stat_exit_value
  445. stat_exit_value()
  446. {
  447. statsfile=`statsfile Exiting_with_return_value "$1"`
  448. good_lines_nb=`grep '(EX' $statsfile | wc -l | tr -d ' '`
  449. grep '(EX' "$statsfile" \
  450. | datamash --sort groupby 6 -W count 5 \
  451. | awk -v good_lines_nb=$good_lines_nb \
  452. '{ printf "%.2g%% %s\n", 100*$2/good_lines_nb, $1 }' \
  453. | sort -n
  454. }
  455. echoq stat_exit_value_by_value
  456. stat_exit_value_by_value()
  457. {
  458. statsfile=`statsfile Exiting_with_return_value "$1"`
  459. datamash --sort groupby 5 -W count 5 < "$statsfile"
  460. }
  461. datamash_file_op_index() {
  462. file="$1"
  463. op="${2:-mean}"
  464. index="${3:-4}" # the four field by default
  465. format="${4:-%16.1f}" # --format=%16.1f by default
  466. func="${5:-}"
  467. val_datamash_file_op_index=`LC_ALL=C datamash --format="$format" -W "$op" "$index" < "$file"`
  468. func_return=
  469. test -n "$func" && func_return=`eval $func $val_datamash_file_op_index`
  470. echo "$val_datamash_file_op_index" $func_return
  471. }
  472. stat_any() {
  473. echo stat_any "$@"
  474. test -f "$1" || { echo "usage: stat_any file index" ; return 1 ; }
  475. file="$1"
  476. index=${2:-4} # the four field by default
  477. func="${3:-}"
  478. for op in \
  479. "min " \
  480. "perc:10 " \
  481. "q1 " \
  482. "median " \
  483. "mean " \
  484. "q3 " \
  485. "perc:90 " \
  486. "max " \
  487. do
  488. echo -n "$file $index $op " ; datamash_file_op_index $file $op $index %16.1f $func
  489. done
  490. echo
  491. }
  492. echoq stat_all
  493. stat_all()
  494. {
  495. stat_load "$1" ; echo
  496. echo G_REMOTE_ADDR_$1.txt
  497. egrep -o 'REMOTE_ADDR is .*' G_REMOTE_ADDR_$1.txt | sort -g | uniq -c | sort -g | tail -5
  498. echo
  499. echo G_REMOTE_HOST_$1.txt
  500. egrep -o 'REMOTE_HOST is .*' G_REMOTE_HOST_$1.txt | sort -g | uniq -c | sort -g | tail -5
  501. echo
  502. echo G_HTTP_COOKIE_$1.txt
  503. egrep -o 'imapsync_runs=[0-9]+' G_HTTP_COOKIE_$1.txt | egrep -o '[0-9]+' | sort -n | tail -1
  504. # stat_any G_HTTP_REFERER.txt
  505. echo
  506. echo G_HTTP_REFERER_$1.txt
  507. egrep -o 'HTTP_REFERER is .*' G_HTTP_REFERER_$1.txt | sort -g | uniq -c | sort -g
  508. echo
  509. echo G_Host1_IMAP_server_$1.txt
  510. cat G_Host1_IMAP_server_$1.txt | datamash -s -W -g 4 count 4 | awk '{ print $2 " " $1 }' | sort -g | tail -5
  511. echo
  512. echo G_Host2_IMAP_server_$1.txt
  513. cat G_Host2_IMAP_server_$1.txt | datamash -s -W -g 4 count 4 | awk '{ print $2 " " $1 }' | sort -g | tail -5
  514. echo
  515. stat_any G_Host1_Nb_messages_$1.txt
  516. stat_any G_Host2_Nb_messages_$1.txt
  517. stat_any G_Messages_transferred_$1.txt
  518. stat_any G_Messages_skipped_$1.txt
  519. stat_any G_Messages_found_in_host1_not_in_host2_$1.txt 9
  520. stat_any G_Messages_found_in_host2_not_in_host1_$1.txt 9
  521. # stat_any G_Folders_synced.txt
  522. egrep -o '[0-9]+/[0-9]+ synced' G_Folders_synced_$1.txt | egrep -o '^[0-9]+' > G_Folders_synced__$1.txt
  523. egrep -o '[0-9]+/[0-9]+ synced' G_Folders_synced_$1.txt | egrep -o '[0-9]+/[0-9]+' | egrep -o '[0-9]+$' > G_Folders_total_seen_$1.txt
  524. stat_any G_Folders_synced__$1.txt 1
  525. stat_any G_Folders_total_seen_$1.txt 1
  526. #
  527. stat_any G_Transfer_time_$1.txt
  528. stat_any G_Host1_Total_size_$1.txt
  529. stat_any G_Host2_Total_size_$1.txt
  530. stat_any G_Total_bytes_transferred_$1.txt 5
  531. stat_any G_Message_rate_$1.txt
  532. stat_any G_Average_bandwidth_rate_$1.txt 5
  533. stat_any G_Biggest_message_$1.txt
  534. stat_any G_Detected_errors_$1.txt 2
  535. #stat_any G_Exiting_with_return_value.txt 5 # GROUP
  536. stat_any G_Memory_consumption_at_the_end_$1.txt 7
  537. stat_any G_Memory_consumption_at_the_end_$1.txt 10
  538. #stat_any G_failure_Error_login.txt
  539. echo cpu time
  540. stat_any G_CPU_time_and_cpu_$1.txt 6
  541. echo '%allcpus'
  542. stat_any G_CPU_time_and_cpu_$1.txt 10
  543. echo G_Host1_banner_$1.txt
  544. server_survey_percent G_Host1_banner_$1.txt | tail -6
  545. echo
  546. echo G_Host2_banner_$1.txt
  547. server_survey_percent G_Host2_banner_$1.txt | tail -6
  548. echo
  549. echo USER_AGENT
  550. percent_stat_useragent_X $1 ;
  551. echo
  552. echo EXIT values
  553. stat_exit_value $1
  554. echo
  555. echo "Data made at" `date -r grep_stats_$1.txt`
  556. }
  557. stat_transfer_time_mean()
  558. {
  559. statsfile=`statsfile Transfer_time "$1"`
  560. datamash_file_op_index "$statsfile" mean
  561. }
  562. stat_throuput_since_day_one_in_days()
  563. {
  564. number_of_syncs=`number_of_syncs "$1"`
  565. days_since_first_use=`days_since_first_use "$1"`
  566. c "$number_of_syncs / $days_since_first_use"
  567. }
  568. stat_queue_mean_old()
  569. {
  570. stat_throuput_since_day_one_in_days=`stat_throuput_since_day_one_in_days`
  571. stat_transfer_time_mean=`stat_transfer_time_mean "$1"`
  572. stat_queue_mean_raw=`c "$stat_throuput_since_day_one_in_days * $stat_transfer_time_mean / 3600 / 24"`
  573. LC_ALL=C printf "%2.2f\n" $stat_queue_mean_raw
  574. }
  575. stat_queue_mean()
  576. {
  577. first_log=`first_log "$1"`
  578. last_log=`last_log "$1"`
  579. number_of_syncs=`number_of_syncs "$1"`
  580. seconds_between_files=`seconds_between_files $first_log $last_log`
  581. stat_transfer_time_mean=`stat_transfer_time_mean "$1"`
  582. stat_queue_mean_raw=`c "$number_of_syncs / $seconds_between_files * $stat_transfer_time_mean"`
  583. LC_ALL=C printf "%2.2f\n" $stat_queue_mean_raw
  584. }
  585. first_log()
  586. {
  587. list_all_logs "$1"_ | grep -a /"$1" | head -1
  588. }
  589. last_log()
  590. {
  591. list_all_logs "$1"_ | grep -a /"$1" | tail -1
  592. }
  593. start_date()
  594. {
  595. first_log=`first_log "$1"`
  596. date -r "$first_log"
  597. }
  598. end_date()
  599. {
  600. last_log=`last_log "$1"`
  601. date -r "$last_log"
  602. }
  603. echoq dirs_of_syncs_finished_recently
  604. dirs_of_syncs_finished_recently() {
  605. find . -maxdepth 1 -mtime "${1:--1}" | grep -v "385d7a4d8d428d7aa2b57c8982629e2bd67698ed" | egrep [a-f0-9]{40} | while read d; do
  606. test -f "$d" && continue
  607. test -f $d/imapsync.pid && continue
  608. echo $d
  609. done
  610. }
  611. echoq 'logfiles_finished_recently -3 # less than 3 days, default is like -1'
  612. logfiles_finished_recently()
  613. {
  614. {
  615. # +2 more than 2 days ago
  616. # -3 less than 3 days ago
  617. # 7 exactly 7 days ago
  618. #set -x
  619. find . -maxdepth 1 -mtime "${1:--1}" | grep -v "385d7a4d8d428d7aa2b57c8982629e2bd67698ed" | egrep [a-f0-9]{40} | while read d; do
  620. test -f "$d" && continue
  621. test -f $d/imapsync.pid && continue
  622. test -d $d/ || continue
  623. ls -trb `find $d/ -type f -mtime "${1:--1}" | grep \.txt`
  624. done
  625. }
  626. }
  627. last_dirs_written()
  628. {
  629. ls -tr | tail -1800
  630. }
  631. last_file_written_in_dir()
  632. {
  633. ls -trd $1/*.txt |tail -1
  634. }
  635. is_dir_running_imapsync()
  636. {
  637. test -d "$1" || return 1
  638. test -f "$1/imapsync.pid" && PID=`head -1 "$1/imapsync.pid"` &&
  639. ps -p $PID -o comm= > /dev/null
  640. }
  641. echoq logfiles_running
  642. logfiles_running()
  643. {
  644. last_dirs_written | while read d
  645. do
  646. is_dir_running_imapsync "$d" &&
  647. last_file_written_in_dir "$d"
  648. done
  649. }
  650. epoch_of_file()
  651. {
  652. date -r "$1" +%s
  653. }
  654. epoch_of_now()
  655. {
  656. date +%s
  657. }
  658. is_file_older_than()
  659. {
  660. # return 1 if not exist or recent
  661. # return 0 if older than "$2" seconds or 15 minutes (900 secondes)
  662. test -f "$1" || return 1
  663. epoch_file=`epoch_of_file "$1"`
  664. epoch_now=`epoch_of_now`
  665. epoch_diff=`expr $epoch_now - $epoch_file`
  666. #echo "$epoch_now - $epoch_file = $epoch_diff"
  667. if test "${2:-900}" -lt "$epoch_diff"
  668. then
  669. #echo older than $2
  670. return 0
  671. else
  672. #echo newer than $2
  673. return 1
  674. fi
  675. }
  676. newer()
  677. {
  678. test -f "$2" || return 0
  679. test "$1" -nt "$2"
  680. }
  681. pids_of_imapsync_not_writing_since_x_secondes()
  682. {
  683. x_secondes=${1:-900} # 15 minutes by default
  684. last_dirs_written | while read d
  685. do
  686. is_dir_running_imapsync "$d" &&
  687. is_file_older_than `last_file_written_in_dir "$d"` "$x_secondes" &&
  688. head -1 "$d/imapsync.pid" | tr '\n' ' '
  689. done
  690. }
  691. kill_HUP_pids_of_imapsync_not_writing_since_x_secondes()
  692. {
  693. pids_not_writing=`pids_of_imapsync_not_writing_since_x_secondes ${1:-900}`
  694. test -n "$pids_not_writing" && echo kill -HUP "$pids_not_writing" # && kill -HUP "$pids_not_writing"
  695. }
  696. watch_logfiles_running_old() {
  697. # the "tail --pid=" option does not exist on FreeBSD, it's GNU/Linux
  698. while date; do
  699. inotifywait /var/tmp/imapsync_cgi -e create 2>/dev/null &
  700. PID_inotifywait=$!
  701. logfiles_running | xargs -d'\n' tail --pid=$PID_inotifywait -f -v
  702. echo "NEW SYNC IS RUNNING"
  703. echo "Syncs running: "; number_and_pids_of_imapsync_running
  704. sleep 3
  705. done
  706. }
  707. watch_logfiles_running_old2() {
  708. while date; do
  709. kill $PID_inotifywait
  710. inotifywait /var/tmp/imapsync_cgi -e create 2>/dev/null &
  711. PID_inotifywait=$!
  712. kill_tail_logfiles_running
  713. tail_logfiles_running
  714. wait $PID_inotifywait
  715. kill_tail_logfiles_running
  716. echo "NEW SYNC IS RUNNING"
  717. echo "Syncs running: "; number_and_pids_of_imapsync_running
  718. sleep 3
  719. done
  720. }
  721. tail_logfiles_running() {
  722. logfiles_running=`logfiles_running`
  723. test -n "$logfiles_running" && tail -f $logfiles_running
  724. #PID_tail_logfiles_running=$!
  725. #fg
  726. }
  727. echoq watch_logfiles_running
  728. watch_logfiles_running() {
  729. tail_logfiles_running
  730. }
  731. kill_tail_logfiles_running() {
  732. kill $PID_tail_logfiles_running
  733. }
  734. echoq watch_new_runs
  735. watch_new_runs() {
  736. while { date; echo -n "Nb syncs currently: " ; number_and_pids_of_imapsync_running ; } do
  737. inotifywait . -e create 2>/dev/null | { read path action f
  738. echo $f
  739. sleep 2
  740. test -f $f/imapsync.pid && PID=`head -1 $f/imapsync.pid` && echo PID $PID
  741. echo -e '\a'
  742. }
  743. done
  744. }
  745. echoq pidfiles_running_and_not_running
  746. pidfiles_running_and_not_running() {
  747. ls -tr | while read f; do
  748. test -f $f/imapsync.pid && PID=`head -1 $f/imapsync.pid` && echo -n "$PID " &&
  749. { ps -p $PID -o comm= | tr '\n' ' ' && { test -f /proc/$PID/oom_score &&
  750. { echo -12 > /proc/$PID/oom_adj ; } && echo -n "oom_score " && cat /proc/$PID/oom_score | tr '\n' ' ' ; : ; }
  751. } &&
  752. { ls -tr $f/*.txt |tail -1 ; }
  753. done
  754. }
  755. pidfile_dandling() {
  756. pidfile_dandling_DIR=$1
  757. test -d $pidfile_dandling_DIR || return 2
  758. test -f $pidfile_dandling_DIR/imapsync.pid || return 3
  759. pidfile_dandling_PID=`head -1 $pidfile_dandling_DIR/imapsync.pid`
  760. #echo "$pidfile_dandling_PID"
  761. test -n "$pidfile_dandling_PID" || return 4
  762. test "$pidfile_dandling_PID" -ge 1 || return 5
  763. if ! ps -p "$pidfile_dandling_PID" -o comm= > /dev/null ; then
  764. #echo -n "DANDLING $pidfile_dandling_DIR/imapsync.pid "
  765. #echo "# PID $pidfile_dandling_PID"
  766. return 0
  767. fi
  768. return 99
  769. }
  770. echoq pidfiles_not_running
  771. pidfiles_not_running() {
  772. ls -tr | while read f; do
  773. if pidfile_dandling "$f" ; then
  774. pidfiles_not_running_PID=`head -1 $f/imapsync.pid`
  775. echo -n "rm $f/imapsync.pid # "
  776. { ls -tr $f/*.txt 2>/dev/null |tail -1 ; } | tr '\n' ' '
  777. echo "# PID $pidfiles_not_running_PID"
  778. #head -2 $f/imapsync.pid
  779. fi
  780. done
  781. }
  782. first_use() {
  783. test -f first_use && cat first_use && return
  784. echo "${1:-2017} ${2:-01} ${3:-09}"
  785. }
  786. filedate()
  787. {
  788. test FreeBSD = `uname -s` && gdate -r "$1" '+%Y %m %d'
  789. test Linux = `uname -s` && date -r "$1" '+%Y %m %d'
  790. }
  791. days_between_files()
  792. {
  793. epoch1=`epoch_of_file "$1"`
  794. epoch2=`epoch_of_file "$2"`
  795. echo epoch1 $epoch1 epoch2 $epoch2
  796. expr \( $epoch2 - $epoch1 \) / 3600 / 24
  797. }
  798. seconds_between_files()
  799. {
  800. epoch1=`epoch_of_file "$1"`
  801. epoch2=`epoch_of_file "$2"`
  802. expr \( $epoch2 - $epoch1 \)
  803. }
  804. days_since_first_use() {
  805. first_use=`first_use "$@"`
  806. #echo $[$[$(date +%s)-$(epoch_of_y_m_d_h_m_s 2017 01 09 00 00 00)]/60/60/24]
  807. echo $[$[$(date +%s)-$(epoch_of_y_m_d_h_m_s $first_use 00 00 00)]/60/60/24]
  808. }
  809. epoch_of_y_m_d_h_m_s() {
  810. date -v -1d > /dev/null 2>&1 && date -u -v ${1:-1970}y -v ${2:-1}m -v ${3:-1}d -v ${4:-0}H -v ${5:-0}M -v ${6:-0}S +%s && return
  811. date --date="1 day ago" > /dev/null && date -u -d "${1:-1970}-${2:-1}-${3:-1} ${4:-0}:${5:-0}:${6:-0}" +%s && return
  812. }
  813. date_x_days_ago() {
  814. date -v -1d > /dev/null 2>&1 && date -u -v -${1:-0}d "+%Y-%m-%d %a" && return
  815. date --date="1 day ago" > /dev/null && date -u --date="${1:-0} day ago" "+%Y-%m-%d %a" && return
  816. }
  817. seconds_to_days_hours() {
  818. #eval "echo $(date -ud "@${1:-0}" +'$((%s/3600/24)) days %_H hours %_M min %_S sec')"
  819. date -v -1d > /dev/null 2>&1 && eval "echo $(date -ur "${1:-0}" +'$((%s/3600/24)) days %_H hours %_M min %_S sec')" && return
  820. date --date="1 day ago" > /dev/null && eval "echo $(date -ud "@${1:-0}" +'$((%s/3600/24)) days %_H hours %_M min %_S sec')" && return
  821. }
  822. seconds_to_days_hours_echo() {
  823. date -v -1d > /dev/null 2>&1 && echo "echo $(date -ur "${1:-0}" +'$((%s/3600/24)) days %_H hours %_M min %_S sec')" && return
  824. date --date="1 day ago" > /dev/null && echo "echo $(date -ud "@${1:-0}" +'$((%s/3600/24)) days %_H hours %_M min %_S sec')" && return
  825. }
  826. printf_this_one_div10()
  827. {
  828. num=$1
  829. printf "% $((num/10))s\n" $1
  830. }
  831. echoq 'runs_per_day 7 # last 7 days'
  832. runs_per_day() {
  833. historic_start=`days_since_first_use`
  834. start=${1:-$historic_start}
  835. for cc in `count 0 $start`; do
  836. DATE=`date_x_days_ago $cc`
  837. # find on FreeBSD finds nothing with -mtime 0
  838. test FreeBSD = `uname -s` && cc=`expr 1 + $cc`
  839. runs_this_day=`find . -maxdepth 1 -mtime $cc -ls |wc -l`
  840. echo -n "$DATE $cc days ago: " ; printf_this_one_div10 $runs_this_day
  841. done
  842. }
  843. echoq summary_run
  844. summary_run() {
  845. for summary_run_DIR in "$@"; do
  846. echo Analysing $summary_run_DIR
  847. echo -n "Nb logs: "; ls $summary_run_DIR/*.txt | wc -l
  848. summary_run_LOGS_LIST=`ls $summary_run_DIR/*.txt`
  849. echo -n "List logs: "; echo $summary_run_LOGS_LIST
  850. #echo connect failure
  851. summary_run_CONNECT_FAIL=`grep -i 'failure: can not open imap connection on' $summary_run_DIR/*.txt|wc -l`
  852. echo CONN $summary_run_CONNECT_FAIL
  853. #echo login failure
  854. grep -i 'failure: Error login on' $summary_run_DIR/*.txt
  855. #echo Differences
  856. grep -i "difference host2 - host1" $summary_run_DIR/*.txt
  857. done
  858. }
  859. logs_nb() {
  860. logs_nb_DIR="$1"
  861. logs_nb_LOGS_LIST="$logs_nb_DIR"/*.txt
  862. }
  863. vnstat_init() {
  864. test FreeBSD = `uname -s` && VNSTATI_DIR=/usr/local/www/apache24/data/vnstat
  865. test Linux = `uname -s` && VNSTATI_DIR=/var/www/html/vnstat
  866. test -d $VNSTATI_DIR || mkdir -p $VNSTATI_DIR
  867. }
  868. echoq vnstat_gen
  869. vnstat_gen() {
  870. vnstat_init || return
  871. for opt in s h hg hs d m y t vs 5 5g ; do
  872. test "$1" && echo vnstati -$opt -o $VNSTATI_DIR/vnstat_${opt}.png
  873. vnstati -$opt -o $VNSTATI_DIR/vnstat_${opt}.png
  874. done
  875. }
  876. echoq vnstat_index_hs
  877. vnstat_index_hs()
  878. {
  879. (
  880. vnstat_init || return
  881. cd $VNSTATI_DIR/ || return
  882. for f in `ls -r ./*/vnstat_hs.png`
  883. do
  884. echo '<img src="'$f'" border="0" alt="hourly"><br>'
  885. done > index_hs.html
  886. )
  887. }
  888. echoq vnstat_archive
  889. vnstat_archive() {
  890. (
  891. vnstat_gen "$1" || return
  892. now_ymdhms=`date +%Y_%m_%d_%H_%M_%S` || return
  893. mkdir $VNSTATI_DIR/$now_ymdhms/ || return
  894. cd $VNSTATI_DIR/$now_ymdhms/ || return
  895. test "$1" && pwd
  896. cp -a ../*.png ../*.html .
  897. )
  898. test "$1" && pwd
  899. }
  900. echoq dstat_csv
  901. dstat_csv() {
  902. #dstat -l -n -cdgyms 60 1
  903. dstat -t -l -n -cdgyms --output dstat.csv 60
  904. }
  905. echoq 'ratio_killed_by_TERM -3 # last 3 days'
  906. ratio_killed_by_TERM() {
  907. logfiles_finished_recently=`logfiles_finished_recently $1`
  908. nb_logfiles_finished_recently=`echo $logfiles_finished_recently | wc -w`
  909. echo -n "Got a signal TERM: " && echo $logfiles_finished_recently | xargs grep -i 'Got a signal TERM' | wc -l
  910. echo -n "Got a signal : " && echo $logfiles_finished_recently | xargs grep -i 'Got a signal' | wc -l
  911. echo -n "Among finished : " && echo $nb_logfiles_finished_recently
  912. echo "logfiles_finished_recently $1 | xargs grep -i 'Got a signal TERM' "
  913. }
  914. echoq 'nb_syncs_badly_finished -1 # last 1 day'
  915. nb_syncs_badly_finished()
  916. {
  917. logfiles_finished_recently=`logfiles_finished_recently $1`
  918. nb_logfiles_finished_recently=`echo $logfiles_finished_recently | wc -w | tr -d ' '`
  919. nb_syncs_badly_finished=`echo $logfiles_finished_recently | xargs grep -i 'Exiting with return value' | grep -v 'return value 0' | wc -l `
  920. echo $nb_syncs_badly_finished / $nb_logfiles_finished_recently \
  921. | awk '{ printf "%s %.2g%% %s\n", "Total:", 100*$1/$3, $0 }'
  922. echo $logfiles_finished_recently | xargs grep -i 'Exiting with return value' \
  923. | grep -v 'return value 0' | grep -o 'Exiting with return value.*)' \
  924. | sort | uniq -c | sort -n \
  925. | awk -v nb_logfiles_finished_recently=$nb_logfiles_finished_recently \
  926. '{ printf "%.2g%% %s\n", 100*$1/nb_logfiles_finished_recently, $0 }'
  927. cat <<EOF
  928. logfiles_finished_recently $1 | xargs grep -i 'Exiting with return value' | grep -v 'return value 0 ' | cut -d: -f1 | xargs tail -11
  929. EOF
  930. }
  931. echoq 'referrer_of_x /var/log/apache/access.log_2022*.gz | sort | uniq -c | sort -n'
  932. referrer_of_x() {
  933. zegrep -h -s -o 'GET /X/? .*http[^"]+' "${@:-/var/log/apache/access.log}" | grep -o 'http.*'
  934. }
  935. biggest_message_seen() {
  936. statsfile=`statsfile Biggest_message $1`
  937. cat "$statsfile" | grep -v Memory | datamash -W max 4 | xargs bytestohuman
  938. }
  939. biggest_message_transferred() {
  940. # With this, the "Biggest message" may be not be transferred by imapsync itself.
  941. statsfile=`statsfile Biggest_message $1`
  942. grep 'Host2 Biggest message' < "$statsfile" | datamash -W max 4 | xargs bytestohuman
  943. }
  944. biggest_bandwidth_rate() {
  945. statsfile=`statsfile Average_bandwidth_rate $1`
  946. datamash_file_op_index "$statsfile" max 5 | tr -d ' ' | tr '\n' ' '
  947. echo KiB/s
  948. }
  949. average_bandwidth_rate() {
  950. statsfile=`statsfile Average_bandwidth_rate $1`
  951. datamash_file_op_index "$statsfile" mean 5 | tr -d ' ' | tr '\n' ' '
  952. echo KiB/s
  953. }
  954. max_number_of_messages_transferred() {
  955. statsfile=`statsfile Messages_transferred $1`
  956. datamash_file_op_index "$statsfile" max 4 "%.0f"
  957. }
  958. max_number_of_messages_skipped() {
  959. statsfile=`statsfile Messages_skipped $1`
  960. datamash_file_op_index "$statsfile" max 4 "%.0f"
  961. }
  962. echoq number_of_X_users
  963. number_of_X_users()
  964. {
  965. statsfile=`statsfile REMOTE_ADDR $1`
  966. test -f $statsfile || { echo No exists $statsfile ; return ; }
  967. datamash_file_op_index $statsfile unique 3 | tr , '\n' | wc -l
  968. }
  969. summary_compute_old() {
  970. list_all_logs_generate \
  971. && grep_stats_from_list_all_logs \
  972. && grep_all_stat_from_patterns_list \
  973. && summary_display
  974. }
  975. echoq "summary_compute `date +%Y_%m_%d` # time depending on lognamepattern"
  976. summary_compute() {
  977. grep_stats_from_list_log_matching "$1" \
  978. && grep_all_stat_from_patterns_list "$1" \
  979. && stat_all "$1" && summary_display "$1"
  980. }
  981. number_of_syncs()
  982. {
  983. list_all_logs | egrep -a "$1"_ | wc -l
  984. }
  985. count_expression()
  986. {
  987. #echo count_expression "[$1]" "[$2]"
  988. egrep -- "$1" "$2" | wc -l
  989. }
  990. patterns_alone_file_generate()
  991. {
  992. patterns_file="$1"
  993. patterns_alone_file="$2"
  994. > $patterns_alone_file
  995. cat $patterns_file |
  996. while read imap_server pattern
  997. do
  998. echo "$pattern" >> $patterns_alone_file
  999. done
  1000. }
  1001. count_imap_server_all()
  1002. {
  1003. count_imap_server_all=0
  1004. cat $patterns_file |
  1005. while read imap_server pattern
  1006. do
  1007. #echo count_imap_server "$pattern" "$banners_files"
  1008. count_imap_server=`count_expression "$pattern" "$banners_files"`
  1009. count_imap_server_all=`expr $count_imap_server_all + $count_imap_server`
  1010. echo $count_imap_server_all
  1011. done
  1012. }
  1013. #echoq server_survey_percent
  1014. server_survey_percent()
  1015. {
  1016. banners_files=${1:-G_Host1_banner.txt}
  1017. patterns_file=${2:-server_survey_patterns.txt}
  1018. patterns_alone_file=${patterns_file}.alone.txt
  1019. patterns_alone_file_generate $patterns_file $patterns_alone_file
  1020. banners_counted=`egrep -f $patterns_alone_file $banners_files | wc -l | tr -d ' \n'`
  1021. banners_not_counted=`egrep -v -f $patterns_alone_file $banners_files | wc -l | tr -d ' \n'`
  1022. banners_all=`cat $banners_files | wc -l | tr -d ' \n'`
  1023. banners_all_verif=`expr $banners_not_counted + $banners_counted`
  1024. cat $patterns_file |
  1025. while read imap_server pattern
  1026. do
  1027. #echo count_imap_server "$pattern" "$banners_files"
  1028. count_imap_server=`count_expression "$pattern" "$banners_files"`
  1029. percent_imap_server=`echo "scale=2; 100 * $count_imap_server/$banners_all" | bc -l`
  1030. echo $percent_imap_server% : $count_imap_server " : $imap_server : " "[$pattern]"
  1031. done | sort -n
  1032. }
  1033. #echoq server_survey
  1034. server_survey()
  1035. {
  1036. banners_files=${1:-G_Host1_banner.txt}
  1037. patterns_file=${2:-server_survey_patterns.txt}
  1038. server_survey_percent $banners_files $patterns_file
  1039. count_imap_server_all=`count_imap_server_all | tail -1`
  1040. echo $banners_files
  1041. echo "Banners counted sum $count_imap_server_all"
  1042. echo "Banners counted $banners_counted"
  1043. echo "Banners not counted $banners_not_counted"
  1044. echo "Banners all $banners_all"
  1045. echo "Banners all verif $banners_all_verif = $banners_not_counted + $banners_counted"
  1046. if test $count_imap_server_all != $banners_counted
  1047. then echo WARNING count_imap_server_all $count_imap_server_all != $banners_counted banners_counted \
  1048. diff `expr $count_imap_server_all - $banners_counted`
  1049. fi
  1050. echo "server_survey $banners_files # finished"
  1051. }
  1052. echoq server_survey_next_pattern
  1053. server_survey_next_pattern()
  1054. {
  1055. patterns_alone_file_generate server_survey_patterns.txt server_survey_patterns.txt.alone.txt
  1056. grep -h -o 'banner:.*' G_Host?_banner.txt |sort | uniq -c | sort -g > banner_counted_sorted.txt
  1057. egrep -v -f server_survey_patterns.txt.alone.txt banner_counted_sorted.txt
  1058. }
  1059. echoq server_survey_last_pattern
  1060. server_survey_last_pattern()
  1061. {
  1062. banners_files1=${1:-G_Host1_banner.txt}
  1063. banners_files2=${2:-G_Host2_banner.txt}
  1064. tail -1 server_survey_patterns.txt > pattern_alone_file.txt
  1065. server_survey $banners_files1 pattern_alone_file.txt
  1066. server_survey $banners_files2 pattern_alone_file.txt
  1067. }
  1068. echoq server_survey_host1
  1069. server_survey_host1()
  1070. {
  1071. server_survey G_Host1_banner.txt
  1072. }
  1073. echoq server_survey_host2
  1074. server_survey_host2()
  1075. {
  1076. server_survey G_Host2_banner.txt
  1077. }
  1078. date_space()
  1079. {
  1080. date | tr -d '\n'
  1081. echo -n " "
  1082. }
  1083. #echoq date_if_new_hour
  1084. date_if_new_hour()
  1085. {
  1086. min=`date +%M`
  1087. sec=`date +%S`
  1088. #echo $min $sec
  1089. if test "00" = "$min" && test 6 -ge $sec
  1090. then
  1091. echo
  1092. date_space
  1093. sleep 1
  1094. fi
  1095. }
  1096. echoq watch_number_of_imapsync_running
  1097. watch_number_of_imapsync_running()
  1098. {
  1099. date_space
  1100. while number_of_imapsync_running | tr -d ' \n'
  1101. do
  1102. sleep 6
  1103. date_if_new_hour
  1104. done
  1105. }
  1106. #echoq number_of_bytes_sent_received_per_second_during
  1107. number_of_bytes_sent_received_per_second_during()
  1108. {
  1109. # $1 : number of seconds to watch
  1110. here_is_freebsd && number_of_bytes_sent_received_per_second_during_freebsd ${1:-1}
  1111. here_is_linux && number_of_bytes_sent_received_per_second_during_linux ${1:-1}
  1112. }
  1113. number_of_bytes_sent_received_per_second_during_freebsd()
  1114. {
  1115. netstat_result=`netstat -I em0 -w ${1:-1} -q 1 | tail -1`
  1116. echo $netstat_result | awk -v sec="${1:-1}" '{ printf "%.0f", ($4+$7)/sec }'
  1117. }
  1118. tx_file_linux()
  1119. {
  1120. test -r /sys/class/net/eth0/statistics/tx_bytes && echo /sys/class/net/eth0/statistics/tx_bytes
  1121. test -r /sys/class/net/eno0/statistics/tx_bytes && echo /sys/class/net/eno0/statistics/tx_bytes
  1122. test -r /sys/class/net/ens3/statistics/tx_bytes && echo /sys/class/net/ens3/statistics/tx_bytes
  1123. }
  1124. rx_file_linux()
  1125. {
  1126. test -r /sys/class/net/eth0/statistics/rx_bytes && echo /sys/class/net/eth0/statistics/rx_bytes
  1127. test -r /sys/class/net/eno0/statistics/rx_bytes && echo /sys/class/net/eno0/statistics/rx_bytes
  1128. test -r /sys/class/net/ens3/statistics/rx_bytes && echo /sys/class/net/ens3/statistics/rx_bytes
  1129. }
  1130. number_of_bytes_sent_received_per_second_during_linux()
  1131. {
  1132. tx_file_linux=`tx_file_linux`
  1133. rx_file_linux=`rx_file_linux`
  1134. tx_1=`cat $tx_file_linux`
  1135. rx_1=`cat $rx_file_linux`
  1136. sleep ${1:-1}
  1137. tx_2=`cat $tx_file_linux`
  1138. rx_2=`cat $rx_file_linux`
  1139. echo "( $tx_2 - $tx_1 + $rx_2 - $rx_1 ) / ${1:-1}" | bc
  1140. }
  1141. div_1_by_2_or_zero()
  1142. {
  1143. if test X"$2" = X"0"; then
  1144. echo "0"
  1145. else
  1146. echo "$1 $2" | awk '{ printf "%.0f\n", $1/$2 }'
  1147. fi
  1148. }
  1149. load_1_minute_linux()
  1150. {
  1151. cat /proc/loadavg | cut -d' ' -f1
  1152. }
  1153. load_1_minute_freebsd()
  1154. {
  1155. /sbin/sysctl vm.loadavg | egrep -o '[0-9]+\.[0-9]++' | head -1
  1156. }
  1157. load_1_minute()
  1158. {
  1159. here_is_linux && load_1_minute_linux
  1160. here_is_freebsd && load_1_minute_freebsd
  1161. }
  1162. KiBytes_to_Bytes()
  1163. {
  1164. expr 1024 \* $1
  1165. }
  1166. number_of_cores_live()
  1167. {
  1168. here_is_linux && cat /proc/cpuinfo | grep ^processor | wc -l && return
  1169. here_is_freebsd && sysctl -n kern.smp.cpus && return
  1170. echo 1
  1171. }
  1172. number_of_cores()
  1173. {
  1174. test -n "$number_of_cores" && echo "$number_of_cores" && return
  1175. number_of_cores=`number_of_cores_live`
  1176. echo "$number_of_cores"
  1177. }
  1178. cpu_slots_available()
  1179. {
  1180. # rule of thumb: 4 imapsync processes per cpu
  1181. echo "scale=0; ( `number_of_cores` - `load_1_minute` ) * 4 / 1 " | bc
  1182. }
  1183. maxint()
  1184. {
  1185. test "$1" -gt "$2" && echo "$1" || echo "$2"
  1186. }
  1187. minint()
  1188. {
  1189. test "$1" -gt "$2" && echo "$2" || echo "$1"
  1190. }
  1191. slots_available()
  1192. {
  1193. minint "`cpu_slots_available`" "`memory_available_in_slots`"
  1194. }
  1195. bandwidth_nominal_bytes_per_second()
  1196. {
  1197. test "$hostname" = vp3 && echo 25000000 # 100 mbps + rx 100 mbps tx
  1198. test "$hostname" = vp4 && echo 25000000 # 100 mbps + rx 100 mbps tx
  1199. test "$hostname" = ks6 && echo 25000000 # 100 mbps + rx 100 mbps tx
  1200. echo 25000000 # default 100 mbps + rx 100 mbps tx
  1201. }
  1202. ratio_percent()
  1203. {
  1204. expr 100 \* $1 / $2
  1205. }
  1206. echoq number_of_imapsync_running_bandwidth
  1207. number_of_imapsync_running_bandwidth()
  1208. {
  1209. # Maybe I could do two number_of_imapsync_running one before
  1210. # one after and average the two.
  1211. number_of_imapsync_running=`number_of_imapsync_running`
  1212. load_1_minute=`load_1_minute`
  1213. bandwidth_bytes_per_sec=`number_of_bytes_sent_received_per_second_during ${1:-1}`
  1214. bandwidth_bytes_per_sec_max=`bandwidth_nominal_bytes_per_second`
  1215. bandwidth_percent=`ratio_percent $bandwidth_bytes_per_sec $bandwidth_bytes_per_sec_max`
  1216. memory_used_by_all_processes_var # sets memory_used_by_all_processes_KiB memory_used_by_all_processes_bytes memory_used_by_all_processes_percent memory_used_by_all_processes_human
  1217. memory_available_var # sets memory_available_in_slots
  1218. ratio=`div_1_by_2_or_zero $bandwidth_bytes_per_sec $number_of_imapsync_running`
  1219. date=`date_ymdhms`
  1220. date_u=`LANG= date -u`
  1221. bandwidth_bytes_per_sec_human=`bytestohuman $bandwidth_bytes_per_sec`
  1222. ratio_human=`bytestohuman $ratio`
  1223. slots_available=`slots_available`
  1224. sleep 0.1
  1225. echo "$date $number_of_imapsync_running $bandwidth_bytes_per_sec $ratio $bandwidth_bytes_per_sec_human $ratio_human $load_1_minute $memory_used_by_all_processes_KiB $memory_used_by_all_processes_percent $bandwidth_percent $slots_available"
  1226. #echo "$hostname Load: $load_1_minute; Syncs: $number_of_imapsync_running; Mem: $memory_used_by_all_processes_human ($memory_used_by_all_processes_percent%); Bandwidth: $bandwidth_bytes_per_sec_human/s ($bandwidth_percent%); Bandwidth per sync: $ratio_human/s; Date/time: $date_u;" >/var/tmp/imapsync_current_old_${1:-1}.txt
  1227. printf "%8s Load: %4s; Syncs: %2s (%2s); Mem: %11s (%3s%%); Bandwidth: %11s/s (%3s%%); Bandwidth per sync: %11s/s; Date/time: %s;\n" \
  1228. "$hostname" "$load_1_minute" "$number_of_imapsync_running" "$slots_available" "$memory_used_by_all_processes_human" "$memory_used_by_all_processes_percent" "$bandwidth_bytes_per_sec_human" "$bandwidth_percent" "$ratio_human" "$date_u" > /var/tmp/imapsync_current_${1:-1}.txt
  1229. echo "$date $hostname $number_of_imapsync_running mem $memory_used_by_all_processes_KiB $memory_used_by_all_processes_percent cpu $load_1_minute bandwidth $bandwidth_bytes_per_sec $bandwidth_percent slots $slots_available" > /var/tmp/imapsync_mem_cpu_bandwidth_${1:-1}.txt
  1230. }
  1231. echoq loop_number_of_imapsync_running_bandwidth
  1232. loop_number_of_imapsync_running_bandwidth()
  1233. {
  1234. while :
  1235. do
  1236. :
  1237. nirbd=`number_of_imapsync_running_bandwidth ${1:-1}`
  1238. echo $nirbd
  1239. echo $nirbd >> /var/tmp/number_of_imapsync_running_every_${1:-1}s.txt
  1240. done
  1241. }
  1242. grep_header()
  1243. {
  1244. echo "$2" | grep "$1" | cut -d: -f2-
  1245. }
  1246. get_header()
  1247. {
  1248. curl -s --head --max-time 4 "$1"
  1249. }
  1250. tests_grep_last_modified()
  1251. {
  1252. run_test test "" = "`grep_last_modified`"
  1253. run_test test "" = "`grep_last_modified abcd`"
  1254. run_test test " Fri, 01 Jul 2022 09:05:38 GMT" = "`grep_last_modified 'Last-Modified: Fri, 01 Jul 2022 09:05:38 GMT'`"
  1255. run_test test " Fri, 01 Jul 2022 09:05:38 GMT" = "`grep_last_modified 'HTTP/1.1 200 OK
  1256. Date: Fri, 01 Jul 2022 09:05:44 GMT
  1257. Server: Apache/2.4.53 (Debian)
  1258. Last-Modified: Fri, 01 Jul 2022 09:05:38 GMT
  1259. ETag: "a6-5e2bab09b5bc2"
  1260. Accept-Ranges: bytes
  1261. Content-Length: 166
  1262. Vary: Accept-Encoding
  1263. Content-Type: text/plain
  1264. '`"
  1265. }
  1266. grep_last_modified()
  1267. {
  1268. grep_header Last-Modified: "$1"
  1269. }
  1270. tests_grep_date()
  1271. {
  1272. run_test test "" = "`grep_date`"
  1273. run_test test "" = "`grep_date abcd`"
  1274. run_test test " Fri, 01 Jul 2022 09:05:44 GMT" = "`grep_date 'Date: Fri, 01 Jul 2022 09:05:44 GMT'`"
  1275. run_test test " Fri, 01 Jul 2022 09:05:44 GMT" = "`grep_date 'HTTP/1.1 200 OK
  1276. Date: Fri, 01 Jul 2022 09:05:44 GMT
  1277. Server: Apache/2.4.53 (Debian)
  1278. Last-Modified: Fri, 01 Jul 2022 09:05:38 GMT
  1279. ETag: "a6-5e2bab09b5bc2"
  1280. Accept-Ranges: bytes
  1281. Content-Length: 166
  1282. Vary: Accept-Encoding
  1283. Content-Type: text/plain
  1284. '`"
  1285. }
  1286. grep_date()
  1287. {
  1288. grep_header Date: "$1"
  1289. }
  1290. tests_date_to_epoch()
  1291. {
  1292. run_test test "1234567890" = "`date_to_epoch 'Fri, 13 Feb 2009 23:31:30 GMT'`"
  1293. run_test test "1234567890" = "`date_to_epoch 'Sat Feb 14 00:31:30 CET 2009'`"
  1294. }
  1295. date_to_epoch()
  1296. {
  1297. here_is_freebsd && { gdate --date "$1" +%s && return ; }
  1298. here_is_linux && { date --date "$1" +%s && return ; }
  1299. }
  1300. tests_diff_current_vs_last_modified()
  1301. {
  1302. run_test test "" = "`diff_current_vs_last_modified`"
  1303. run_test test "" = "`diff_current_vs_last_modified blabla`"
  1304. run_test test "" = "`diff_current_vs_last_modified blabla blibli`"
  1305. # No "Last-Modified:"
  1306. run_test test "" = "`diff_current_vs_last_modified 'HTTP/1.1 200 OK
  1307. Date: Fri, 01 Jul 2022 09:05:44 GMT
  1308. Server: Apache/2.4.53 (Debian)
  1309. '`"
  1310. # No "Date:"
  1311. run_test test "" = "`diff_current_vs_last_modified 'HTTP/1.1 200 OK
  1312. Last-Modified: Fri, 01 Jul 2022 09:05:38 GMT
  1313. '`"
  1314. # Both "Date:" and "Last-Modified:"
  1315. run_test test "6" = "`diff_current_vs_last_modified 'HTTP/1.1 200 OK
  1316. Date: Fri, 01 Jul 2022 09:05:44 GMT
  1317. Server: Apache/2.4.53 (Debian)
  1318. Last-Modified: Fri, 01 Jul 2022 09:05:38 GMT
  1319. ETag: "a6-5e2bab09b5bc2"
  1320. Accept-Ranges: bytes
  1321. Content-Length: 166
  1322. Vary: Accept-Encoding
  1323. Content-Type: text/plain
  1324. '`"
  1325. }
  1326. diff_current_vs_last_modified()
  1327. {
  1328. test -n "$1" || { echo "" && return ; }
  1329. last_modified=`grep_last_modified "$1"`
  1330. test -n "$last_modified" || { echo "" && return ; }
  1331. current_date=`grep_date "$1"`
  1332. test -n "$current_date" || { echo "" && return ; }
  1333. last_modified_epoch=`date_to_epoch "$last_modified"`
  1334. current_date_epoch=`date_to_epoch "$current_date"`
  1335. echo $current_date_epoch - $last_modified_epoch | bc
  1336. }
  1337. freshness()
  1338. {
  1339. header_to_check=`get_header "$1"`
  1340. diff_current_vs_last_modified "$header_to_check"
  1341. }
  1342. imapsync_spots_single()
  1343. {
  1344. :
  1345. freshness=`freshness "$1"`
  1346. test -n "$freshness" || return
  1347. if test 30 -gt "$freshness"
  1348. then
  1349. get_url "$1"
  1350. fi
  1351. }
  1352. imapsync_spots_available()
  1353. {
  1354. imapsync_hosts=`curl --silent https://i005.lamiral.info/imapsync_remote.txt`
  1355. #echo $imapsync_hosts
  1356. for imapsync_host in $imapsync_hosts
  1357. do
  1358. imapsync_spots_link=`https_and_host_part "$imapsync_host"`/imapsync_mem_cpu_bandwidth_6.txt
  1359. #echo $imapsync_spots_link
  1360. imapsync_spots_single "$imapsync_spots_link"
  1361. done | egrep -o 'slots [0-9]+' | awk '{ sum+=$2 } END { print sum }'
  1362. }
  1363. add_spare_if_needed()
  1364. {
  1365. :
  1366. imapsync_spots_available=`imapsync_spots_available`
  1367. echo "imapsync_spots_available=$imapsync_spots_available"
  1368. if test 0 -gt "$imapsync_spots_available"
  1369. then
  1370. add_spare
  1371. fi
  1372. }
  1373. imapsync_spare_running()
  1374. {
  1375. imapsync_spare_host=`curl --silent https://i005.lamiral.info/imapsync_remote_spare.txt`
  1376. #echo $imapsync_spare_host
  1377. test -n "$imapsync_spare_host" || return
  1378. imapsync_spare_link=`https_and_host_part "$imapsync_spare_host"`/imapsync_mem_cpu_bandwidth_6.txt
  1379. #echo $imapsync_spare_link
  1380. imapsync_spots_single "$imapsync_spare_link" | cut -d' ' -f3
  1381. }
  1382. remove_spare_if_not_needed()
  1383. {
  1384. imapsync_spare_running=`imapsync_spare_running`
  1385. imapsync_spots_available=`imapsync_spots_available`
  1386. echo imapsync_spare_running = $imapsync_spare_running
  1387. echo imapsync_spots_available = $imapsync_spots_available
  1388. if test "0" = "$imapsync_spare_running" && test "1" -lt "$imapsync_spots_available"
  1389. then
  1390. echo remove_spare
  1391. remove_spare
  1392. fi
  1393. }
  1394. remove_spare()
  1395. {
  1396. imapsync_remote_spare_be_void
  1397. echo wait_runs_to_be_finished to be coded
  1398. shelve_spare
  1399. }
  1400. add_spare()
  1401. {
  1402. unshelve_spare
  1403. imapsync_remote_spare_be_i050
  1404. }
  1405. imapsync_remote_spare_be_i050()
  1406. {
  1407. ssh root@i005.lamiral.info 'ln -sfv /var/tmp/imapsync_remote_i050.txt /var/tmp/imapsync_remote_spare.txt'
  1408. }
  1409. imapsync_remote_spare_be_void()
  1410. {
  1411. ssh root@i005.lamiral.info 'ln -sfv /var/tmp/imapsync_remote_void.txt /var/tmp/imapsync_remote_spare.txt'
  1412. }
  1413. unshelve_spare()
  1414. {
  1415. openstack server list
  1416. check_i050 && echo "i050 is already up and running" && return 0
  1417. openstack server unshelve i050
  1418. openstack server list
  1419. openstack server show i050
  1420. while ! ping -c 1 i050.lamiral.info ; do : ; done
  1421. sleep 10
  1422. check_i050 && echo "i050 is up and running" || { echo "i050 failed waking up" ; return 1 ; }
  1423. }
  1424. shelve_spare()
  1425. {
  1426. ssh root@i050.lamiral.info halt
  1427. while ping -c 1 i050.lamiral.info ; do sleep 1 ; done
  1428. echo i050 is down
  1429. openstack server shelve i050
  1430. openstack server show i050
  1431. openstack server list
  1432. echo openstack server list
  1433. }
  1434. check_i050()
  1435. {
  1436. curl -v --max-time 7 --data 'testslive=1;exitonload=0' https://i050.lamiral.info/cgi-bin/imapsync 2>/dev/null | grep 'Exiting with return value 0'
  1437. }
  1438. loop_add_or_remove_spare()
  1439. {
  1440. while :
  1441. do
  1442. :
  1443. imapsync_spare_running=`imapsync_spare_running`
  1444. imapsync_spots_available=`imapsync_spots_available`
  1445. echo imapsync_spare_running = $imapsync_spare_running
  1446. echo imapsync_spots_available = $imapsync_spots_available
  1447. add_or_remove_spare "$imapsync_spots_available" "$imapsync_spare_running" | tee -a /var/tmp/add_or_remove_spare_$$.txt
  1448. sleep 60
  1449. done
  1450. }
  1451. tests_add_or_remove_spare()
  1452. {
  1453. dothis=echo
  1454. run_test test "add_spare" = "`add_or_remove_spare -2 ''`" #
  1455. run_test test "add_spare" = "`add_or_remove_spare -1 ''`" #
  1456. run_test test "add_spare" = "`add_or_remove_spare 0 ''`" #
  1457. run_test test "echo can sleep" = "`add_or_remove_spare 1 ''`" #
  1458. run_test test "echo can sleep" = "`add_or_remove_spare 2 ''`" #
  1459. run_test test "remove_spare" = "`add_or_remove_spare 5 0`" #
  1460. run_test test "remove_spare" = "`add_or_remove_spare 2 0`" #
  1461. run_test test "echo can sleep" = "`add_or_remove_spare 0 0`" #
  1462. run_test test "echo can sleep" = "`add_or_remove_spare 1 0`" #
  1463. run_test test "echo can sleep" = "`add_or_remove_spare 5 5`" #
  1464. }
  1465. add_or_remove_spare()
  1466. {
  1467. :
  1468. spots_available="$1"
  1469. spare_running="$2"
  1470. #echo spots_available="$1" spare_running="$2"
  1471. # No spot available, no spare running
  1472. if test 0 -ge "$spots_available" && test "" = "$spare_running"
  1473. then
  1474. $dothis add_spare
  1475. # At leat 2 spots available, no spare running
  1476. elif test 2 -le "$spots_available" && test 0 = "$spare_running"
  1477. then
  1478. $dothis remove_spare
  1479. else
  1480. $dothis echo can sleep
  1481. fi
  1482. }
  1483. openstack_memo()
  1484. {
  1485. :
  1486. cat << EOF
  1487. openstack server list
  1488. openstack server show eaf08fae-afd5-4354-ab2f-345357fea83b
  1489. openstack server unshelve eaf08fae-afd5-4354-ab2f-345357fea83b
  1490. openstack server shelve eaf08fae-afd5-4354-ab2f-345357fea83b
  1491. # need to be unshelved
  1492. openstack console log show eaf08fae-afd5-4354-ab2f-345357fea83b
  1493. EOF
  1494. }
  1495. echoq imapsync_current
  1496. imapsync_current()
  1497. {
  1498. imapsync_hosts=`curl --silent https://i005.lamiral.info/imapsync_remote.txt https://i005.lamiral.info/imapsync_remote_spare.txt https://i005.lamiral.info/imapsync_remote_extra.txt`
  1499. #echo $imapsync_hosts
  1500. for imapsync_host in $imapsync_hosts
  1501. do
  1502. imapsync_current_link=`https_and_host_part "$imapsync_host"`/imapsync_current_6.txt
  1503. #echo $imapsync_current_link
  1504. imapsync_current_single "$imapsync_current_link"
  1505. done
  1506. }
  1507. imapsync_current_save()
  1508. {
  1509. :
  1510. imapsync_current | tee /var/tmp/imapsync_current_6s_all.txt.tmp
  1511. mv /var/tmp/imapsync_current_6s_all.txt.tmp /var/tmp/imapsync_current_6s_all.txt
  1512. }
  1513. https_and_host_part()
  1514. {
  1515. echo "$1" | egrep -o 'https://[^/]+'
  1516. }
  1517. get_url()
  1518. {
  1519. curl --silent --max-time 2 "$1"
  1520. }
  1521. imapsync_current_single()
  1522. {
  1523. :
  1524. freshness=`freshness "$1"`
  1525. test -n "$freshness" || return
  1526. if test 30 -gt "$freshness"
  1527. then
  1528. get_url "$1"
  1529. else
  1530. :
  1531. echo "$freshness"
  1532. fi
  1533. }
  1534. echoq loop_imapsync_current
  1535. loop_imapsync_current()
  1536. {
  1537. while :
  1538. do
  1539. imapsync_current_save
  1540. echo
  1541. sleep 30
  1542. done
  1543. }
  1544. echoq generate_proximapsync_stuff
  1545. generate_proximapsync_stuff()
  1546. {
  1547. newer imapsync_form.js proximapsync_form.js &&
  1548. {
  1549. echo generating proximapsync_form.js
  1550. sed 's/cgi-bin\/imapsync/cgi-bin\/proximapsync/' imapsync_form.js > proximapsync_form.js
  1551. echo | ci -f -l -m"from imapsync_form.js" proximapsync_form.js
  1552. }
  1553. newer imapsync_form_extra.html proximapsync_form_extra.html &&
  1554. {
  1555. echo generating proximapsync_form_extra.html
  1556. sed 's/cgi-bin\/imapsync/cgi-bin\/proximapsync/' imapsync_form_extra.html > proximapsync_form_extra.html
  1557. sed -i 's/imapsync_form.js/proximapsync_form.js/' proximapsync_form_extra.html
  1558. echo | ci -f -l -m"from imapsync_form_extra.html" proximapsync_form_extra.html
  1559. }
  1560. newer imapsync_form_extra_free.html proximapsync_form_extra_free.html &&
  1561. {
  1562. echo generating proximapsync_form_extra_free.html
  1563. sed 's/cgi-bin\/imapsync/cgi-bin\/proximapsync/' imapsync_form_extra_free.html > proximapsync_form_extra_free.html
  1564. sed -i 's/imapsync_form.js/proximapsync_form.js/' proximapsync_form_extra_free.html
  1565. echo | ci -f -l -m"from imapsync_form_extra_free.html" proximapsync_form_extra_free.html
  1566. }
  1567. }
  1568. echoq summary_display lognamepattern
  1569. summary_display() {
  1570. echo "Start date : " `start_date "$1"`
  1571. echo "End date : " `end_date "$1"`
  1572. echo -n "Number of /X users: " ; number_of_X_users "$1"
  1573. echo -n "Number of /X accounts synced: " ; nb_migrations_launched "$1"
  1574. echo -n "Number of /X syncs: " ; number_of_syncs "$1"
  1575. echo -n "Total volume /X transferred: " ; total_volume_transferred "$1"
  1576. echo -n "Total messages /X transferred: " ; total_messages_transferred "$1"
  1577. echo -n "Biggest transfer: " ; biggest_transfer "$1"
  1578. echo -n "Biggest message seen: " ; biggest_message_seen "$1"
  1579. echo -n "Biggest message transferred: " ; biggest_message_transferred "$1"
  1580. echo -n "Biggest bandwidth rate: " ; biggest_bandwidth_rate "$1"
  1581. echo -n "Average bandwidth rate: " ; average_bandwidth_rate "$1"
  1582. echo -n "Max messages transferred: " ; max_number_of_messages_transferred "$1"
  1583. echo -n "Max messages skipped: " ; max_number_of_messages_skipped "$1"
  1584. echo -n "Longest transfer: " ; seconds_to_days_hours `longest_transfer "$1"`
  1585. echo -n "Queue length mean is: " ; stat_queue_mean "$1"
  1586. echo "Data made at" `date -r grep_stats_"$1".txt`
  1587. }
  1588. echoq various_usefull
  1589. various_usefull() {
  1590. cat <<'EOF'
  1591. strace -e trace=signal -f `pgrep /usr/sbin/apach | xargs -n1 echo -n " -p "` 2>&1
  1592. egrep -o '* ID .*' G_Read___ID.txt | sort | uniq -c | sort -n
  1593. egrep -o '* ID .*' G_Read___ID.txt | sort | awk '{ print $1 " " $2 " " $3 " NIL" }' | datamash -s -W -g 3 count 3 | awk '{ print $2 " " $1 }' | sort -g
  1594. locate perl.core | xargs -n 1 gdb -q -x /tmp/gdb_quit.txt -c
  1595. zcat /var/log/apache/httpd-access.log.*.gz|egrep -o -w 'Mozilla/5.0 \([^;]+' | sort | egrep -o '\([a-zA-Z]+' | sort | uniq -c | sort -g | grep -v KHTML
  1596. zcat /var/log/apache/httpd-access.log.*.gz|grep 'POST /cgi-bin/imapsync' | egrep -o -w 'Mozilla/5.0 \([^;]+' | sort | egrep -o '\([a-zA-Z]+' | sort | uniq -c | sort -g | grep -v KHTML
  1597. egrep -o '\[.+@[^]]+]' G_success_login.txt |head -222222 | sort | uniq -c | sort -g
  1598. cat G_success_login_on.txt | ./domains_servers_map | sort | uniq -c | sort -g
  1599. list_all_logs |tail -9999 | xargs grep -i 'Exiting with return value 112' | tee Error_112_last_9999_syncs.txt
  1600. cut -d: -f1 Error_112_last_30_days.txt | xargs grep -oih 'Invalid system flag.*' | sort | uniq -c
  1601. list_all_logs | xargs grep -i 'Exiting with return value 112' | tee Error_112_all_syncs.txt
  1602. cut -d: -f1 Error_112_all_syncs.txt | tail -100 | xargs egrep -oih 'Invalid system flag [^(]+' | sort | uniq -c
  1603. logfiles_finished_recently -300| xargs grep -i 'Exiting with return value 10 ' | grep -v 'return value 0 ' | cut -d: -f1 | xargs tail -11 | grep 'failure: can not open imap connection on' | uniq -c | sort -g | grep http | tee ../http_host_failures.txt
  1604. # Searching big messages copied over 500 MB
  1605. list_all_logs|tail -50000 | xargs egrep '{.?[56789]........} copied'
  1606. # online processes stats
  1607. cat /var/tmp/number_of_imapsync_running_every_60s.txt | datamash -W min 2 max 2 mean 2 median 2 q1 2 q3 2
  1608. for v in 2 3 4; do cat /var/tmp/number_of_imapsync_running_every_6s.txt | datamash --format=%10.0f -W min $v max $v mean $v median $v q1 $v q3 $v ; done
  1609. netstat -I em0 -b -n -w 6 -q 1
  1610. while :; do ssh root@ks5 'cd /var/tmp/imapsync_cgi/ ; . cgi_memo ; loop_number_of_imapsync_running_bandwidth 6' ; echo $?; done
  1611. # Search memory eater
  1612. cat G_Memory_consumption_at_the_end.txt | sort -g -k7 | grep 202[01] |tail -100 | cut -f1 -d: | while read f; do echo $f ; grep 'Memory consumption at the end' $f; grep 'Host. Nb messages' $f ; grep 'Biggest message' $f ; grep 'Memory/biggest message ratio' $f ; done
  1613. cat G_Host2_Nb_messages.txt | sort -g -k4 | grep 202[01] |tail -100 | cut -f1 -d: | while read f; do echo $f ; grep 'Memory consumption at the end' $f; grep 'Host. Nb messages' $f ; grep 'Biggest message' $f ; grep 'Memory/biggest message ratio' $f ; done
  1614. cat G_Host1_Nb_messages.txt | sort -g -k4 | grep 202[01] |tail -100 | cut -f1 -d: | while read f; do echo $f ; grep 'Memory consumption at the end' $f; grep 'Host. Nb messages' $f ; grep 'Biggest message' $f ; grep 'Memory/biggest message ratio' $f ; done
  1615. # Best bandwidth moments
  1616. cat /var/tmp/number_of_imapsync_running_every_6s.txt | sort -k3 -g| tail -66
  1617. # Worst load momemts
  1618. cat /var/tmp/number_of_imapsync_running_every_6s.txt | sort -k9 -g| tail -66
  1619. # Sort by number of parallel runs and by load in case of equality
  1620. cat /var/tmp/number_of_imapsync_running_every_6s.txt | grep ^2022_ | sort -k2 -k9 -g | tail -666
  1621. # Sort by number of parallel runs and by bandwidth in case of equality
  1622. cat /var/tmp/number_of_imapsync_running_every_6s.txt | grep ^2022_ | sort -k2 -k3 -g | tail -666
  1623. # getrusage on FreeBSD, espacially disk i/o
  1624. procstat -r `pgrep -f cgi-bin/imapsync`
  1625. # Sum up all memory taken by imapsync runs, in KiB.
  1626. ps -o rss -p `pgrep -f cgi-bin/imapsync` | sed 1,1d | datamash sum 1
  1627. memory_used_by_all_imapsync_KiB
  1628. #
  1629. systemctl --no-pager status apache2 ; systemctl --no-pager restart apache2 ; systemctl --no-pager status apache2
  1630. systemctl --no-pager status httpd ; systemctl --no-pager restart httpd ; systemctl --no-pager status httpd
  1631. # Search for 'Client-Aborted: die X-Died: EOF when chunk header expected' and for what hosts
  1632. tail -66666 list_all_logs_auto.txt | grep -v 385d7a4d8d428d7aa2b57c8982629e2bd67698ed|egrep -o '.*/'|sort | uniq | while read d; do echo $d/*_proxy.txt ; done | grep -v '*' | xargs egrep -l 'Client-Aborted: die X-Died: EOF when chunk header expected' | xargs grep 'Gonna delegate the imap sync to'
  1633. EOF
  1634. }
  1635. echoq perf_help
  1636. perf_help() {
  1637. test FreeBSD = `uname -s` && {
  1638. echo FreeBSD here
  1639. echo "nload -t 6000 em0 -u K -i 100000 -o 100000"
  1640. echo "iftop -i em0 -f 'port imap or port imaps' -B # t p >"
  1641. }
  1642. test Linux = `uname -s` && {
  1643. echo Linux here
  1644. echo "nload -t 6000 eth0 -u K -i 100000 -o 100000 # Linux"
  1645. echo "iftop -i eth0 -f 'port imap or port imaps' -B # t p >"
  1646. }
  1647. }
  1648. tests_all_verbose_if_failure
  1649. init()
  1650. {
  1651. hostname=`hostname`
  1652. }
  1653. init