v-list-sys-services 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. state='running'
  20. # Searching related pids
  21. if [ -z $3 ]; then
  22. pids=$(pidof $proc_name |tr ' ' '|')
  23. else
  24. pids=$(pidof -x $proc_name |tr ' ' '|')
  25. fi
  26. if [ ! -z "$pids" ]; then
  27. pid=$(echo $pids|cut -f 1 -d \|)
  28. pids=$(egrep "$pids" $tmp_file)
  29. # Calculating CPU usage
  30. cpu=$(echo "$pids" |awk '{ sum += $2} END {print sum}')
  31. # Calculating memory usage
  32. mem=$(echo "$pids" |awk '{sum += $3} END {print sum/1024 }')
  33. mem=$(printf "%.0f\n" $mem)
  34. # Searching service uptime
  35. if [ -e "/var/run/$srv.pid" ]; then
  36. srv_file="/var/run/$srv.pid"
  37. fi
  38. if [ -z "$srv_file" ] && [ -e "/var/run/$srv/$srv.pid" ]; then
  39. srv_file="/var/run/$srv/$srv.pid"
  40. fi
  41. if [ -z $srv_file ] && [ -e "/proc/$pid" ]; then
  42. srv_file="/proc/$pid"
  43. fi
  44. if [ ! -z "$srv_file" ]; then
  45. mtime=$(stat -c "%Y" $srv_file)
  46. rtime=$((ctime - mtime))
  47. rtime=$((rtime / 60))
  48. else
  49. rtime=0
  50. fi
  51. else
  52. # Service is stopped
  53. state='stopped'
  54. mem=0
  55. cpu=0
  56. rtime="0"
  57. fi
  58. }
  59. #----------------------------------------------------------#
  60. # Action #
  61. #----------------------------------------------------------#
  62. # Save current proccess list
  63. tmp_file=$(mktemp)
  64. ps -eo pid,pcpu,size > $tmp_file
  65. # Get current time
  66. ctime=$(date +%s)
  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="NAME='$service' SYSTEM='web server' STATE='$state' CPU='$cpu'"
  75. str="$str MEM='$mem' RTIME='$rtime'"
  76. fi
  77. # Proxy
  78. service=$PROXY_SYSTEM
  79. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  80. get_srv_state $service
  81. str="$str\nNAME='$service' SYSTEM='reverse proxy' STATE='$state' CPU='$cpu'"
  82. str="$str MEM='$mem' RTIME='$rtime'"
  83. fi
  84. # DNS
  85. service=$DNS_SYSTEM
  86. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  87. if [ "$service" == 'bind' ] || [ "$service" == 'bind9' ]; then
  88. service='named'
  89. fi
  90. get_srv_state $service
  91. str="$str\nNAME='$service' SYSTEM='dns server' STATE='$state' CPU='$cpu'"
  92. str="$str MEM='$mem' RTIME='$rtime'"
  93. fi
  94. # MAIL
  95. service=$MAIL_SYSTEM
  96. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  97. get_srv_state $service
  98. str="$str\nNAME='$service' SYSTEM='mail server' STATE='$state' CPU='$cpu'"
  99. str="$str MEM='$mem' RTIME='$rtime'"
  100. fi
  101. # IMAP
  102. service=$IMAP_SYSTEM
  103. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  104. get_srv_state $service
  105. str="$str\nNAME='$service' SYSTEM='pop/imap server' STATE='$state'"
  106. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  107. fi
  108. # ANTIVIRUS
  109. service=$ANTIVIRUS_SYSTEM
  110. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  111. if [ -e "/etc/redhat-release" ]; then
  112. if [ "$ANTIVIRUS_SYSTEM" = 'clamav' ];then
  113. service='clamd'
  114. fi
  115. get_srv_state $service
  116. else
  117. if [ "$ANTIVIRUS_SYSTEM" = 'clamav-daemon' ];then
  118. clam_proc_name='clamd'
  119. fi
  120. get_srv_state $service $clam_proc_name
  121. fi
  122. str="$str\nNAME='$service' SYSTEM='email antivirus' STATE='$state'"
  123. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  124. fi
  125. # ANTISPAM
  126. service=$ANTISPAM_SYSTEM
  127. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  128. get_srv_state $service spamd
  129. str="$str\nNAME='$service' SYSTEM='email antispam' STATE='$state'"
  130. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  131. fi
  132. # DB
  133. service=$DB_SYSTEM
  134. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  135. for db in ${DB_SYSTEM//,/ }; do
  136. service="$db"
  137. if [ "$service" == 'mysql' ]; then
  138. db_proc_name='mysqld'
  139. fi
  140. if [ "$service" == 'pgsql' ]; then
  141. service='postgresql'
  142. db_proc_name='postmaster'
  143. if [ ! -e "/etc/redhat-release" ]; then
  144. db_proc_name='postgres'
  145. fi
  146. fi
  147. get_srv_state $service $db_proc_name
  148. str="$str\nNAME='$service' SYSTEM='database server' STATE='$state'"
  149. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  150. done
  151. fi
  152. # FTP
  153. service=$FTP_SYSTEM
  154. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  155. get_srv_state $service
  156. str="$str\nNAME='$service' SYSTEM='ftp server' STATE='$state' CPU='$cpu'"
  157. str="$str MEM='$mem' RTIME='$rtime'"
  158. fi
  159. # CRON
  160. service=$CRON_SYSTEM
  161. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  162. get_srv_state $service
  163. str="$str\nNAME='$service' SYSTEM='job scheduler' STATE='$state'"
  164. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  165. fi
  166. # FIREWALL
  167. service=$FIREWALL_SYSTEM
  168. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  169. state="stopped"
  170. /sbin/iptables -L vesta >/dev/null 2>&1
  171. if [ "$?" -eq 0 ]; then
  172. state="running"
  173. fi
  174. str="$str\nNAME='$FIREWALL_SYSTEM' SYSTEM='firewall'"
  175. str="$str STATE='$state' CPU='0' MEM='0' RTIME='0'"
  176. fi
  177. # Fail2ban
  178. service=$FIREWALL_EXTENSION
  179. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  180. get_srv_state $service fail2ban-server script
  181. str="$str\nNAME='$service' SYSTEM='brute-force monitor' STATE='$state'"
  182. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  183. fi
  184. # Defining config
  185. echo -e "$str" > $tmp_file
  186. conf=$tmp_file
  187. # Defining fileds to select
  188. fields="\$NAME \$SYSTEM \$STATE \$CPU \$MEM \$RTIME"
  189. # Listing services
  190. case $format in
  191. json) json_list ;;
  192. plain) nohead=1; shell_list ;;
  193. shell) fields='$NAME $STATE $CPU $MEM $RTIME'
  194. shell_list | column -t ;;
  195. *) check_args '1' '0' 'USER [FORMAT]'
  196. esac
  197. rm -f $tmp_file
  198. #----------------------------------------------------------#
  199. # Vesta #
  200. #----------------------------------------------------------#
  201. exit