v-list-sys-services 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. # Backend
  78. service=$WEB_BACKEND
  79. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  80. get_srv_state $service
  81. str="$str\nNAME='$service' SYSTEM='backend server' STATE='$state' CPU='$cpu'"
  82. str="$str MEM='$mem' RTIME='$rtime'"
  83. fi
  84. # Proxy
  85. service=$PROXY_SYSTEM
  86. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  87. get_srv_state $service
  88. str="$str\nNAME='$service' SYSTEM='reverse proxy' STATE='$state' CPU='$cpu'"
  89. str="$str MEM='$mem' RTIME='$rtime'"
  90. fi
  91. # DNS
  92. service=$DNS_SYSTEM
  93. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  94. if [ "$service" == 'bind' ] || [ "$service" == 'bind9' ]; then
  95. service='named'
  96. fi
  97. get_srv_state $service
  98. str="$str\nNAME='$service' SYSTEM='dns server' STATE='$state' CPU='$cpu'"
  99. str="$str MEM='$mem' RTIME='$rtime'"
  100. fi
  101. # MAIL
  102. service=$MAIL_SYSTEM
  103. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  104. get_srv_state $service
  105. str="$str\nNAME='$service' SYSTEM='mail server' STATE='$state' CPU='$cpu'"
  106. str="$str MEM='$mem' RTIME='$rtime'"
  107. fi
  108. # IMAP
  109. service=$IMAP_SYSTEM
  110. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  111. get_srv_state $service
  112. str="$str\nNAME='$service' SYSTEM='pop/imap server' STATE='$state'"
  113. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  114. fi
  115. # ANTIVIRUS
  116. service=$ANTIVIRUS_SYSTEM
  117. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  118. if [ -e "/etc/redhat-release" ]; then
  119. if [ "$ANTIVIRUS_SYSTEM" = 'clamav' ];then
  120. service='clamd'
  121. fi
  122. get_srv_state $service
  123. else
  124. if [ "$ANTIVIRUS_SYSTEM" = 'clamav-daemon' ];then
  125. clam_proc_name='clamd'
  126. fi
  127. get_srv_state $service $clam_proc_name
  128. fi
  129. str="$str\nNAME='$service' SYSTEM='email antivirus' STATE='$state'"
  130. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  131. fi
  132. # ANTISPAM
  133. service=$ANTISPAM_SYSTEM
  134. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  135. get_srv_state $service spamd
  136. str="$str\nNAME='$service' SYSTEM='email antispam' STATE='$state'"
  137. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  138. fi
  139. # DB
  140. service=$DB_SYSTEM
  141. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  142. for db in ${DB_SYSTEM//,/ }; do
  143. service="$db"
  144. if [ "$service" == 'mysql' ]; then
  145. db_proc_name='mysqld'
  146. fi
  147. if [ "$service" == 'pgsql' ]; then
  148. service='postgresql'
  149. db_proc_name='postmaster'
  150. if [ ! -e "/etc/redhat-release" ]; then
  151. db_proc_name='postgres'
  152. fi
  153. fi
  154. get_srv_state $service $db_proc_name
  155. str="$str\nNAME='$service' SYSTEM='database server' STATE='$state'"
  156. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  157. done
  158. fi
  159. # FTP
  160. service=$FTP_SYSTEM
  161. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  162. get_srv_state $service
  163. str="$str\nNAME='$service' SYSTEM='ftp server' STATE='$state' CPU='$cpu'"
  164. str="$str MEM='$mem' RTIME='$rtime'"
  165. fi
  166. # CRON
  167. service=$CRON_SYSTEM
  168. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  169. get_srv_state $service
  170. str="$str\nNAME='$service' SYSTEM='job scheduler' STATE='$state'"
  171. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  172. fi
  173. # FIREWALL
  174. service=$FIREWALL_SYSTEM
  175. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  176. state="stopped"
  177. /sbin/iptables -L vesta >/dev/null 2>&1
  178. if [ "$?" -eq 0 ]; then
  179. state="running"
  180. fi
  181. str="$str\nNAME='$FIREWALL_SYSTEM' SYSTEM='firewall'"
  182. str="$str STATE='$state' CPU='0' MEM='0' RTIME='0'"
  183. fi
  184. # Fail2ban
  185. service=$FIREWALL_EXTENSION
  186. if [ ! -z "$service" ] && [ "$service" != 'remote' ]; then
  187. get_srv_state $service fail2ban-server script
  188. str="$str\nNAME='$service' SYSTEM='brute-force monitor' STATE='$state'"
  189. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  190. fi
  191. # Defining config
  192. echo -e "$str" > $tmp_file
  193. conf=$tmp_file
  194. # Defining fileds to select
  195. fields="\$NAME \$SYSTEM \$STATE \$CPU \$MEM \$RTIME"
  196. # Listing services
  197. case $format in
  198. json) json_list ;;
  199. plain) nohead=1; shell_list ;;
  200. shell) fields='$NAME $STATE $CPU $MEM $RTIME'
  201. shell_list | column -t ;;
  202. *) check_args '1' '0' 'USER [FORMAT]'
  203. esac
  204. rm -f $tmp_file
  205. #----------------------------------------------------------#
  206. # Vesta #
  207. #----------------------------------------------------------#
  208. exit