v-list-sys-services 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. /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 $(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 ' '|sed "s/^0//")
  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" ]; then
  34. mtime=$(stat -c "%Y" /proc/$pid)
  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" != 'remote' ]; 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" != 'remote' ]; 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" != 'remote' ]; 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" != 'remote' ]; 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" != 'remote' ]; 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" != 'remote' ]; then
  102. if [ -e "/etc/redhat-release" ]; then
  103. if [ "$ANTIVIRUS_SYSTEM" = 'clamav' ];then
  104. service='clamd'
  105. fi
  106. get_srv_state $service
  107. else
  108. if [ "$ANTIVIRUS_SYSTEM" = 'clamav-daemon' ];then
  109. clam_proc_name='clamd'
  110. fi
  111. get_srv_state $service $clam_proc_name
  112. fi
  113. str="$str\nNAME='$service' SYSTEM='email antivirus' STATE='$state'"
  114. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  115. fi
  116. # ANTISPAM
  117. service=$ANTISPAM_SYSTEM
  118. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  119. get_srv_state $service spamd
  120. str="$str\nNAME='$service' SYSTEM='email antispam' STATE='$state'"
  121. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  122. fi
  123. # DB
  124. service=$DB_SYSTEM
  125. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  126. for db in ${DB_SYSTEM//,/ }; do
  127. service="$db"
  128. if [ "$service" == 'mysql' ]; then
  129. if [ ! -e "/etc/init.d/$service" ]; then
  130. service='mysqld'
  131. fi
  132. if [ ! -e "/etc/redhat-release" ]; then
  133. db_proc_name='mysqld'
  134. fi
  135. fi
  136. if [ "$service" == 'pgsql' ]; then
  137. service='postgresql'
  138. db_proc_name='postmaster'
  139. if [ ! -e "/etc/redhat-release" ]; then
  140. db_proc_name='postgres'
  141. fi
  142. fi
  143. get_srv_state $service $db_proc_name
  144. str="$str\nNAME='$service' SYSTEM='database server' STATE='$state'"
  145. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  146. done
  147. fi
  148. # FTP
  149. service=$FTP_SYSTEM
  150. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  151. get_srv_state $service
  152. str="$str\nNAME='$service' SYSTEM='ftp server' STATE='$state' CPU='$cpu'"
  153. str="$str MEM='$mem' RTIME='$rtime'"
  154. fi
  155. # CRON
  156. service=$CRON_SYSTEM
  157. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  158. get_srv_state $service
  159. str="$str\nNAME='$service' SYSTEM='job scheduler' STATE='$state'"
  160. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  161. fi
  162. # Defining config
  163. echo -e "$str" > $tmp_file
  164. conf=$tmp_file
  165. # Defining fileds to select
  166. fields="\$NAME \$SYSTEM \$STATE \$CPU \$MEM \$RTIME"
  167. # Listing services
  168. case $format in
  169. json) json_list ;;
  170. plain) nohead=1; shell_list ;;
  171. shell) fields='$NAME $STATE $CPU $MEM $RTIME'
  172. shell_list | column -t ;;
  173. *) check_args '1' '0' 'USER [FORMAT]'
  174. esac
  175. rm -f $tmp_file
  176. #----------------------------------------------------------#
  177. # Vesta #
  178. #----------------------------------------------------------#
  179. exit