v-list-sys-services 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #!/bin/bash
  2. # info: list system services
  3. # options: [FORMAT]
  4. #
  5. # The function for obtaining the list of configured system services.
  6. #----------------------------------------------------------#
  7. # Variable&Function #
  8. #----------------------------------------------------------#
  9. # Argument defenition
  10. format=${1-shell}
  11. # Includes
  12. source $VESTA/func/main.sh
  13. source $VESTA/conf/vesta.conf
  14. export PATH=$PATH:/sbin
  15. get_srv_state() {
  16. srv=$1
  17. proc_name=${2-$1}
  18. # Check service status
  19. status=$(service $srv status 2>/dev/null)
  20. rc=$?
  21. stopped=$(echo $status| grep stop)
  22. if [ "$rc" -eq 0 ] && [ -z "$stopped" ]; then
  23. state='running'
  24. # Calculate cpu and memory usage
  25. cpu=0
  26. mem=0
  27. for pid in $(pidof $proc_name); do
  28. pid_mem=$(pmap -x $pid | tail -n1 | awk '{print $3}')
  29. pid_cpu=$(grep "^$pid " $tmp_file | cut -f 2 -d ' '|sed "s/^0//")
  30. cpu=$((cpu + pid_cpu))
  31. mem=$((mem + pid_mem))
  32. done
  33. mem=$((mem / 1024))
  34. # Get pid date
  35. if [ ! -z $pid ] && [ -e "/proc/$pid" ]; then
  36. mtime=$(stat -c "%Y" /proc/$pid)
  37. rtime=$((ctime - mtime))
  38. rtime=$((rtime / 60))
  39. fi
  40. else
  41. # Service is stopped
  42. state='stopped'
  43. mem=0
  44. cpu=0
  45. rtime="0"
  46. fi
  47. }
  48. #----------------------------------------------------------#
  49. # Action #
  50. #----------------------------------------------------------#
  51. # Save current proccess list
  52. tmp_file=$(mktemp)
  53. if [ "$format" = 'json' ]; then
  54. ps aux | awk '{print $2" "$3}' | tr -d '.' > $tmp_file
  55. else
  56. ps aux | awk '{print $2" "$3}' | cut -f 1 -d '.' > $tmp_file
  57. fi
  58. # Get current time
  59. ctime=$(date +%s)
  60. # Proxy
  61. service=$PROXY_SYSTEM
  62. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  63. get_srv_state $service
  64. str="NAME='$service' SYSTEM='reverse proxy' STATE='$state' CPU='$cpu'"
  65. str="$str MEM='$mem' RTIME='$rtime'"
  66. fi
  67. # Web
  68. service=$WEB_SYSTEM
  69. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  70. if [ "$service" == 'apache' ]; then
  71. service='httpd'
  72. fi
  73. get_srv_state $service
  74. str="$str\nNAME='$service' SYSTEM='web server' STATE='$state' CPU='$cpu'"
  75. str="$str MEM='$mem' RTIME='$rtime'"
  76. fi
  77. # DNS
  78. service=$DNS_SYSTEM
  79. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  80. if [ "$service" == 'bind' ]; then
  81. service='named'
  82. fi
  83. get_srv_state $service
  84. str="$str\nNAME='$service' SYSTEM='dns server' STATE='$state' CPU='$cpu'"
  85. str="$str MEM='$mem' RTIME='$rtime'"
  86. fi
  87. # MAIL
  88. service=$MAIL_SYSTEM
  89. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  90. get_srv_state $service
  91. str="$str\nNAME='$service' SYSTEM='mail server' STATE='$state' CPU='$cpu'"
  92. str="$str MEM='$mem' RTIME='$rtime'"
  93. fi
  94. # IMAP
  95. service=$IMAP_SYSTEM
  96. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  97. get_srv_state $service
  98. str="$str\nNAME='$service' SYSTEM='pop/imap server' STATE='$state'"
  99. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  100. fi
  101. # ANTIVIRUS
  102. service=$ANTIVIRUS_SYSTEM
  103. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  104. if [ -e "/etc/redhat-release" ]; then
  105. if [ "$ANTIVIRUS_SYSTEM" = 'clamav' ];then
  106. service='clamd'
  107. fi
  108. get_srv_state $service
  109. else
  110. if [ "$ANTIVIRUS_SYSTEM" = 'clamav-daemon' ];then
  111. clam_proc_name='clamd'
  112. fi
  113. get_srv_state $service $clam_proc_name
  114. fi
  115. str="$str\nNAME='$service' SYSTEM='email antivirus' STATE='$state'"
  116. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  117. fi
  118. # ANTISPAM
  119. service=$ANTISPAM_SYSTEM
  120. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  121. get_srv_state $service spamd
  122. str="$str\nNAME='$service' SYSTEM='email antispam' STATE='$state'"
  123. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  124. fi
  125. # DB
  126. service=$DB_SYSTEM
  127. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  128. for db in ${DB_SYSTEM//,/ }; do
  129. service="$db"
  130. if [ "$service" == 'mysql' ]; then
  131. if [ ! -e "/etc/init.d/$service" ]; then
  132. service='mysqld'
  133. fi
  134. if [ ! -e "/etc/redhat-release" ]; then
  135. db_proc_name='mysqld'
  136. fi
  137. fi
  138. if [ "$service" == 'pgsql' ]; then
  139. service='postgresql'
  140. db_proc_name='postmaster'
  141. if [ ! -e "/etc/redhat-release" ]; then
  142. db_proc_name='postgres'
  143. fi
  144. fi
  145. get_srv_state $service $db_proc_name
  146. str="$str\nNAME='$service' SYSTEM='database server' STATE='$state'"
  147. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  148. done
  149. fi
  150. # FTP
  151. service=$FTP_SYSTEM
  152. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  153. get_srv_state $service
  154. str="$str\nNAME='$service' SYSTEM='ftp server' STATE='$state' CPU='$cpu'"
  155. str="$str MEM='$mem' RTIME='$rtime'"
  156. fi
  157. # CRON
  158. service=$CRON_SYSTEM
  159. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  160. get_srv_state $service
  161. str="$str\nNAME='$service' SYSTEM='job scheduler' STATE='$state'"
  162. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  163. fi
  164. # FIREWALL
  165. service=$FIREWALL_SYSTEM
  166. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  167. state="stopped"
  168. /sbin/iptables -L vesta >/dev/null 2>&1
  169. if [ "$?" -eq 0 ]; then
  170. state="running"
  171. fi
  172. str="$str\nNAME='$FIREWALL_SYSTEM' SYSTEM='firewall'"
  173. str="$str STATE='$state' CPU='0' MEM='0' RTIME='0'"
  174. fi
  175. # Defining config
  176. echo -e "$str" > $tmp_file
  177. conf=$tmp_file
  178. # Defining fileds to select
  179. fields="\$NAME \$SYSTEM \$STATE \$CPU \$MEM \$RTIME"
  180. # Listing services
  181. case $format in
  182. json) json_list ;;
  183. plain) nohead=1; shell_list ;;
  184. shell) fields='$NAME $STATE $CPU $MEM $RTIME'
  185. shell_list | column -t ;;
  186. *) check_args '1' '0' 'USER [FORMAT]'
  187. esac
  188. rm -f $tmp_file
  189. #----------------------------------------------------------#
  190. # Vesta #
  191. #----------------------------------------------------------#
  192. exit