v-list-sys-services 7.1 KB

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