v-list-sys-services 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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/conf/vesta.conf
  13. source $VESTA/func/main.sh
  14. get_srv_state() {
  15. srv=$1
  16. proc_name=${2-$1}
  17. # Check service status
  18. /etc/init.d/$srv status > /dev/null 2>&1
  19. if [ $? -eq 0 ]; then
  20. state='running'
  21. # Calculate cpu and memory usage
  22. cpu=0
  23. mem=0
  24. for pid in $(/sbin/pidof $proc_name); do
  25. pid_mem=$(pmap -x $pid | tail -n1 | awk '{print $3}')
  26. pid_cpu=$(grep "^$pid " $tmp_file | cut -f 2 -d ' ')
  27. cpu=$((cpu + pid_cpu))
  28. mem=$((mem + pid_mem))
  29. done
  30. mem=$((mem / 1024))
  31. # Get pid date
  32. if [ ! -z $pid ] && [ -e "/proc/$pid" ]; then
  33. mtime=$(stat -c "%Y" /proc/$pid)
  34. rtime=$((ctime - mtime))
  35. rtime=$((rtime / 60))
  36. fi
  37. else
  38. # Service is stopped
  39. state='stopped'
  40. mem=0
  41. cpu=0
  42. rtime="0"
  43. fi
  44. }
  45. #----------------------------------------------------------#
  46. # Action #
  47. #----------------------------------------------------------#
  48. # Save current proccess list
  49. tmp_file=$(mktemp)
  50. if [ "$format" = 'json' ]; then
  51. ps aux | awk '{print $2" "$3}' | tr -d '.' > $tmp_file
  52. else
  53. ps aux | awk '{print $2" "$3}' | cut -f 1 -d '.' > $tmp_file
  54. fi
  55. # Get current time
  56. ctime=$(date +%s)
  57. # Proxy
  58. service=$PROXY_SYSTEM
  59. if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
  60. get_srv_state $service
  61. str="NAME='$service' SYSTEM='reverse proxy' STATE='$state' CPU='$cpu'"
  62. str="$str MEM='$mem' RTIME='$rtime'"
  63. fi
  64. # Web
  65. service=$WEB_SYSTEM
  66. if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
  67. if [ "$service" == 'apache' ]; then
  68. service='httpd'
  69. fi
  70. get_srv_state $service
  71. str="$str\nNAME='$service' SYSTEM='web server' STATE='$state' CPU='$cpu'"
  72. str="$str MEM='$mem' RTIME='$rtime'"
  73. fi
  74. # DNS
  75. service=$DNS_SYSTEM
  76. if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
  77. if [ "$service" == 'bind' ]; then
  78. service='named'
  79. fi
  80. get_srv_state $service
  81. str="$str\nNAME='$service' SYSTEM='dns server' STATE='$state' CPU='$cpu'"
  82. str="$str MEM='$mem' RTIME='$rtime'"
  83. fi
  84. # MAIL
  85. service=$MAIL_SYSTEM
  86. if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
  87. get_srv_state $service
  88. str="$str\nNAME='$service' SYSTEM='mail server' STATE='$state' CPU='$cpu'"
  89. str="$str MEM='$mem' RTIME='$rtime'"
  90. fi
  91. # IMAP
  92. service=$IMAP_SYSTEM
  93. if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
  94. get_srv_state $service
  95. str="$str\nNAME='$service' SYSTEM='pop/imap server' STATE='$state'"
  96. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  97. fi
  98. # ANTIVIRUS
  99. service=$ANTIVIRUS_SYSTEM
  100. if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
  101. if [ "$ANTIVIRUS_SYSTEM" = 'clamav' ]; then
  102. service='clamd'
  103. fi
  104. get_srv_state $service
  105. str="$str\nNAME='$service' SYSTEM='email antivirus' STATE='$state'"
  106. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  107. fi
  108. # ANTISPAM
  109. service=$ANTISPAM_SYSTEM
  110. if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
  111. get_srv_state $service spamd
  112. str="$str\nNAME='$service' SYSTEM='email antispam' STATE='$state'"
  113. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  114. fi
  115. # DB
  116. service=$DB_SYSTEM
  117. if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
  118. for db in ${DB_SYSTEM//,/ }; do
  119. service="$db"
  120. if [ "$service" == 'mysql' ] && [ ! -e "/etc/init.d/$service" ]; then
  121. service='mysqld'
  122. fi
  123. get_srv_state $service
  124. str="$str\nNAME='$service' SYSTEM='database server' STATE='$state'"
  125. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  126. done
  127. fi
  128. # FTP
  129. service=$FTP_SYSTEM
  130. if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
  131. get_srv_state $service
  132. str="$str\nNAME='$service' SYSTEM='ftp server' STATE='$state' CPU='$cpu'"
  133. str="$str MEM='$mem' RTIME='$rtime'"
  134. fi
  135. # CRON
  136. service=$CRON_SYSTEM
  137. if [ ! -z "$service" ] && [ "$service" != 'no' ]; then
  138. get_srv_state $service
  139. str="$str\nNAME='$service' SYSTEM='job scheduler' STATE='$state'"
  140. str="$str CPU='$cpu' MEM='$mem' RTIME='$rtime'"
  141. fi
  142. # Defining config
  143. echo -e "$str" > $tmp_file
  144. conf=$tmp_file
  145. # Defining fileds to select
  146. fields="\$NAME \$SYSTEM \$STATE \$CPU \$MEM \$RTIME"
  147. # Listing services
  148. case $format in
  149. json) json_list ;;
  150. plain) nohead=1; shell_list ;;
  151. shell) fields='$NAME $STATE $CPU $MEM $RTIME'
  152. shell_list | column -t ;;
  153. *) check_args '1' '0' 'USER [FORMAT]'
  154. esac
  155. rm -f $tmp_file
  156. #----------------------------------------------------------#
  157. # Vesta #
  158. #----------------------------------------------------------#
  159. exit