v-list-sys-services 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <<<<<<< HEAD
  2. #!/bin/bash
  3. # info: list system config
  4. # options: [FORMAT]
  5. #
  6. # The function for obtaining the list of system parameters.
  7. #----------------------------------------------------------#
  8. # Variable&Function #
  9. #----------------------------------------------------------#
  10. # Argument defenition
  11. format=${1-shell}
  12. # Includes
  13. source $VESTA/conf/vesta.conf
  14. source $VESTA/func/main.sh
  15. get_srv_state() {
  16. srv=$1
  17. proc_name=${2-$1}
  18. # Check service status
  19. /etc/init.d/$srv status > /dev/null 2>&1
  20. if [ $? -eq 0 ]; then
  21. state='running'
  22. # Calculate cpu and memory usage
  23. cpu=0
  24. mem=0
  25. for pid in $(/sbin/pidof $proc_name); do
  26. pid_mem=$(pmap -x $pid | tail -n1 | awk '{print $3}')
  27. pid_cpu=$(grep "^$pid " $tmp_file | cut -f 2 -d ' ')
  28. cpu=$((cpu + pid_cpu))
  29. mem=$((mem + pid_mem))
  30. done
  31. mem=$((mem / 1024))
  32. # Get pid date
  33. if [ ! -z $pid ] && [ -e "/proc/$pid/cmdline" ]; then
  34. mtime=$(stat -c "%Y" /proc/$pid/cmdline)
  35. rtime=$((ctime - mtime))
  36. rtime=$((rtime / 60))
  37. fi
  38. else
  39. # Service is stopped
  40. state='stopped'
  41. mem=0
  42. cpu=0
  43. rtime="0"
  44. fi
  45. }
  46. #----------------------------------------------------------#
  47. # Action #
  48. #----------------------------------------------------------#
  49. # Save current proccess list
  50. tmp_file=$(mktemp)
  51. if [ "$format" = 'json' ]; then
  52. ps aux | awk '{print $2" "$3}' | tr -d '.' > $tmp_file
  53. else
  54. ps aux | awk '{print $2" "$3}' | cut -f 1 -d '.' > $tmp_file
  55. fi
  56. # Get current time
  57. ctime=$(date +%s)
  58. # Proxy
  59. service=$PROXY_SYSTEM
  60. if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
  61. get_srv_state $service
  62. str="NAME='$service' SYSTEM='reverse proxy' STATE='$state' CPU='$cpu'"
  63. str="$str MEM='$mem' RTIME='$rtime'"
  64. fi
  65. # Web
  66. service=$WEB_SYSTEM
  67. if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
  68. if [ "$service" == 'apache' ]; then
  69. service='httpd'
  70. fi
  71. get_srv_state $service
  72. str="$str\nNAME='$service' SYSTEM='web server' STATE='$state' CPU='$cpu'"
  73. str="$str MEM='$mem' RTIME='$rtime'"
  74. fi
  75. # DNS
  76. service=$DNS_SYSTEM
  77. if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
  78. if [ "$service" == 'bind' ]; then
  79. service='named'
  80. fi
  81. get_srv_state $service
  82. str="$str\nNAME='$service' SYSTEM='dns server' STATE='$state' CPU='$cpu'"
  83. str="$str MEM='$mem' RTIME='$rtime'"
  84. fi
  85. # MAIL
  86. service=$MAIL_SYSTEM
  87. if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
  88. get_srv_state $service
  89. str="$str\nNAME='$service' SYSTEM='mail server' STATE='$state' CPU='$cpu'"
  90. str="$str MEM='$mem' RTIME='$rtime'"
  91. fi
  92. # IMAP
  93. service=$IMAP_SYSTEM
  94. if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
  95. get_srv_state $service
  96. str="$str\nNAME='$service' SYSTEM='pop/imap server' STATE='$state'"
  97. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  98. fi
  99. # ANTIVIRUS
  100. service=$ANTIVIRUS_SYSTEM
  101. if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
  102. if [ "$ANTIVIRUS_SYSTEM" = 'clamav' ]; then
  103. service='clamd'
  104. fi
  105. get_srv_state $service
  106. str="$str\nNAME='$service' SYSTEM='email antivirus' STATE='$state'"
  107. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  108. fi
  109. # ANTISPAM
  110. service=$ANTISPAM_SYSTEM
  111. if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
  112. get_srv_state $service spamd
  113. str="$str\nNAME='$service' SYSTEM='email antispam' STATE='$state'"
  114. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  115. fi
  116. # DB
  117. service=$DB_SYSTEM
  118. if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
  119. for db in ${DB_SYSTEM//,/ }; do
  120. service="$db"
  121. if [ "$service" == 'mysql' ] && [ ! -e "/etc/init.d/$service" ]; then
  122. service='mysqld'
  123. fi
  124. get_srv_state $service
  125. str="$str\nNAME='$service' SYSTEM='database server' STATE='$state'"
  126. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  127. done
  128. fi
  129. # FTP
  130. service=$FTP_SYSTEM
  131. if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
  132. get_srv_state $service
  133. str="$str\nNAME='$service' SYSTEM='ftp server' STATE='$state' CPU='$cpu'"
  134. str="$str MEM='$mem' RTIME='$rtime'"
  135. fi
  136. # CRON
  137. service=$CRON_SYSTEM
  138. if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
  139. get_srv_state $service
  140. str="$str\nNAME='$service' SYSTEM='job scheduler' STATE='$state'"
  141. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  142. fi
  143. # Defining config
  144. echo -e "$str" > $tmp_file
  145. conf=$tmp_file
  146. # Defining fileds to select
  147. fields="\$NAME \$SYSTEM \$STATE \$CPU \$MEM \$RTIME"
  148. # Listing services
  149. case $format in
  150. json) json_list ;;
  151. plain) nohead=1; shell_list ;;
  152. shell) fields='$NAME $STATE $CPU $MEM $RTIME'
  153. shell_list | column -t ;;
  154. *) check_args '1' '0' 'USER [FORMAT]'
  155. esac
  156. rm -f $tmp_file
  157. #----------------------------------------------------------#
  158. # Vesta #
  159. #----------------------------------------------------------#
  160. exit
  161. =======
  162. #!/bin/bash
  163. # info: list system services
  164. # options: [FORMAT]
  165. #
  166. # The function for obtaining the list of configured system services.
  167. #----------------------------------------------------------#
  168. # Variable&Function #
  169. #----------------------------------------------------------#
  170. # Argument defenition
  171. format=${1-shell}
  172. # Includes
  173. source $VESTA/conf/vesta.conf
  174. source $VESTA/func/main.sh
  175. get_srv_state() {
  176. srv=$1
  177. proc_name=${2-$1}
  178. # Check service status
  179. /etc/init.d/$srv status > /dev/null 2>&1
  180. if [ $? -eq 0 ]; then
  181. state='running'
  182. # Calculate cpu and memory usage
  183. cpu=0
  184. mem=0
  185. for pid in $(/sbin/pidof $proc_name); do
  186. pid_mem=$(pmap -x $pid | tail -n1 | awk '{print $3}')
  187. pid_cpu=$(grep "^$pid " $tmp_file | cut -f 2 -d ' ')
  188. cpu=$((cpu + pid_cpu))
  189. mem=$((mem + pid_mem))
  190. done
  191. mem=$((mem / 1024))
  192. # Get pid date
  193. if [ ! -z $pid ] && [ -e "/proc/$pid/cmdline" ]; then
  194. mtime=$(stat -c "%Y" /proc/$pid/cmdline)
  195. rtime=$((ctime - mtime))
  196. rtime=$((rtime / 60))
  197. fi
  198. else
  199. # Service is stopped
  200. state='stopped'
  201. mem=0
  202. cpu=0
  203. rtime="0"
  204. fi
  205. }
  206. #----------------------------------------------------------#
  207. # Action #
  208. #----------------------------------------------------------#
  209. # Save current proccess list
  210. tmp_file=$(mktemp)
  211. if [ "$format" = 'json' ]; then
  212. ps aux | awk '{print $2" "$3}' | tr -d '.' > $tmp_file
  213. else
  214. ps aux | awk '{print $2" "$3}' | cut -f 1 -d '.' > $tmp_file
  215. fi
  216. # Get current time
  217. ctime=$(date +%s)
  218. # Proxy
  219. service=$PROXY_SYSTEM
  220. if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
  221. get_srv_state $service
  222. str="NAME='$service' SYSTEM='reverse proxy' STATE='$state' CPU='$cpu'"
  223. str="$str MEM='$mem' RTIME='$rtime'"
  224. fi
  225. # Web
  226. service=$WEB_SYSTEM
  227. if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
  228. if [ "$service" == 'apache' ]; then
  229. service='httpd'
  230. fi
  231. get_srv_state $service
  232. str="$str\nNAME='$service' SYSTEM='web server' STATE='$state' CPU='$cpu'"
  233. str="$str MEM='$mem' RTIME='$rtime'"
  234. fi
  235. # DNS
  236. service=$DNS_SYSTEM
  237. if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
  238. if [ "$service" == 'bind' ]; then
  239. service='named'
  240. fi
  241. get_srv_state $service
  242. str="$str\nNAME='$service' SYSTEM='dns server' STATE='$state' CPU='$cpu'"
  243. str="$str MEM='$mem' RTIME='$rtime'"
  244. fi
  245. # MAIL
  246. service=$MAIL_SYSTEM
  247. if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
  248. get_srv_state $service
  249. str="$str\nNAME='$service' SYSTEM='mail server' STATE='$state' CPU='$cpu'"
  250. str="$str MEM='$mem' RTIME='$rtime'"
  251. fi
  252. # IMAP
  253. service=$IMAP_SYSTEM
  254. if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
  255. get_srv_state $service
  256. str="$str\nNAME='$service' SYSTEM='pop/imap server' STATE='$state'"
  257. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  258. fi
  259. # ANTIVIRUS
  260. service=$ANTIVIRUS_SYSTEM
  261. if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
  262. if [ "$ANTIVIRUS_SYSTEM" = 'clamav' ]; then
  263. service='clamd'
  264. fi
  265. get_srv_state $service
  266. str="$str\nNAME='$service' SYSTEM='email antivirus' STATE='$state'"
  267. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  268. fi
  269. # ANTISPAM
  270. service=$ANTISPAM_SYSTEM
  271. if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
  272. get_srv_state $service spamd
  273. str="$str\nNAME='$service' SYSTEM='email antispam' STATE='$state'"
  274. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  275. fi
  276. # DB
  277. service=$DB_SYSTEM
  278. if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
  279. for db in ${DB_SYSTEM//,/ }; do
  280. service="$db"
  281. if [ "$service" == 'mysql' ] && [ ! -e "/etc/init.d/$service" ]; then
  282. service='mysqld'
  283. fi
  284. get_srv_state $service
  285. str="$str\nNAME='$service' SYSTEM='database server' STATE='$state'"
  286. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  287. done
  288. fi
  289. # FTP
  290. service=$FTP_SYSTEM
  291. if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
  292. get_srv_state $service
  293. str="$str\nNAME='$service' SYSTEM='ftp server' STATE='$state' CPU='$cpu'"
  294. str="$str MEM='$mem' RTIME='$rtime'"
  295. fi
  296. # CRON
  297. service=$CRON_SYSTEM
  298. if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
  299. get_srv_state $service
  300. str="$str\nNAME='$service' SYSTEM='job scheduler' STATE='$state'"
  301. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  302. fi
  303. # Defining config
  304. echo -e "$str" > $tmp_file
  305. conf=$tmp_file
  306. # Defining fileds to select
  307. fields="\$NAME \$SYSTEM \$STATE \$CPU \$MEM \$RTIME"
  308. # Listing services
  309. case $format in
  310. json) json_list ;;
  311. plain) nohead=1; shell_list ;;
  312. shell) fields='$NAME $STATE $CPU $MEM $RTIME'
  313. shell_list | column -t ;;
  314. *) check_args '1' '0' 'USER [FORMAT]'
  315. esac
  316. rm -f $tmp_file
  317. #----------------------------------------------------------#
  318. # Vesta #
  319. #----------------------------------------------------------#
  320. exit
  321. >>>>>>> 6bd00c9e5ea0ba53c998291dec65f9d20a9ce49b