使用monit监控Java程序

4
我写信是想得到一些关于如何通过 Monit 启动我的 Java 程序的帮助。我已经编写了一个启动脚本程序.sh。Monit 代码和脚本代码都在此帖子中给出。
问题是,我不能使用通过 Monit 执行的脚本文件启动和停止程序。如果我使用终端启动它,我可以监视该进程,但我无法使用 Monit 启动/停止它。来自 Monit 的日志显示“启动失败”。
然而,我可以轻松地从 Monit 启动和停止诸如 ssh 等程序。Monit 在 sudo 下运行,而我正在以具有管理员特权的帐户运行脚本。
如果有人能帮助我解决这个问题,那将非常有帮助。谢谢
monitrc 文件
#++++++++++#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#Monit settings
set daemon 10 with start delay 2            # check services at 2-minute intervals
set logfile syslog facility log_daemon                      
set logfile /var/log/monit.log
set idfile /var/lib/monit/id
set statefile /var/lib/monit/state
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Mail Server
set mailserver smtp.gmail.com port 587
    username "monit.abc123@gmail.com" password "password"
    using tlsv1 with timeout 30 seconds

set eventqueue
      basedir /var/lib/monit/events # set the base directory where events will be stored
      slots 100                     # optionally limit the queue size


set alert abc123@gmail.com                       # receive all alerts
set alert abc123@gmail.com only on { timeout }   # receive just service-
#                                                # timeout alert
#set alert foo@bar { nonexist, timeout, resource, icmp, connection }
#set alert security@bar on { checksum, permission, uid, gid }
# setup the email for the SMS thing over here.......................
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
set httpd port 2813 and
#     use address localhost  # only accept connection from localhost
     allow localhost        # allow localhost to connect to the server and
     allow 0.0.0.0/0.0.0.0
     allow admin:monit      # require user 'admin' with password 'monit'
#    allow @monit           # allow users of group 'monit' to connect (rw)
#    allow @users readonly  # allow users of group 'users' to connect readonly
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
set mail-format {
    from: monit@$HOST
    subject: Monit Alert --  $EVENT $SERVICE
    message: $EVENT Service $SERVICE
                 Date:        $DATE
                 Action:      $ACTION
                 Host:        $HOST
                 Description: $DESCRIPTION

            Your faithful employee,
            Monit
}
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#***********************************************************************************************
#Computer Resources check
check system myhost.mydomain.tld
    if loadavg (1min) > 4 for 5 cycles then alert
    if loadavg (5min) > 2 for 5 cycles then alert
    if memory usage > 75% for 3 cycles then alert
    if swap usage > 25% for 5 cycles then alert
    if cpu usage (user) > 70% for 5 cycles then alert
    if cpu usage (system) > 70% for 5 cycles then alert
    if cpu usage (wait) > 20% for 5 cycles then alert
#***********************************************************************************************


################################################################################################
#Monitoring SSH Service

check process ssh123 with pidfile /var/run/sshd.pid
start program = "/etc/init.d/ssh start"
stop program = "/etc/init.d/ssh stop"
if cpu > 50% for 5 cycles then alert
if totalmem > 200 MB for 5 cycles then alert
if children > 2 then alert
#if loadavg(5min) greater than 10 for 8 cycles then stop
#if 5 restarts within 5 cycles then timeout

################################################################################################
#Monitoring Prorgam in Java

check process javaprg with pidfile /home/user/Desktop/Binaries/javaprg.pid
start program = "/home/user/Desktop/Binaries/javaprg.sh start"
stop program = "/home/user/Desktop/Binaries/javaprg.sh stop"  
if cpu > 50% for 5 cycles then alert
if totalmem > 1500 MB for 5 cycles then alert
if children > 2 then alert
#if loadavg(5min) greater than 10 for 8 cycles then stop
#if 5 restarts within 5 cycles then timeout

启动/停止脚本

#!/bin/bash
 case $1 in
    start)
       echo $$ > javaprg.pid;
       exec /usr/bin/java -jar javaprg.jar
       ;;
     stop) 
       kill $(cat javaprg.pid);
       rm javaprg.pid
       ;;
     *) 
       echo "usage: javaprg {start|stop}" ;;
 esac
 exit 0
3个回答

1

在您的启动停止脚本中应设置绝对路径。

您可以尝试使用根Shell sudo -s来启动它。

并且您应该考虑使用/etc/monit/conf.d文件夹来放置您的配置文件。


成功使用绝对路径解决了问题。非常抱歉回复晚了! - Sparrow

0
我使用你的脚本时遇到了同样的问题,原因是启动脚本没有指定保存PID的位置。它将javaprg.pid保存到/而不是主文件夹。将启动脚本更改为'echo $$ > /home/usr/binaries/javaprg.pid'即可解决问题。

0
当我尝试在Monit下配置一个shell脚本时,我遇到了同样的问题。解决问题的方法是在程序本身之前使用/bin/sh。

尝试使用以下命令: start program = "/home/user/Desktop/Binaries/javaprg.sh start" stop program = "/home/user/Desktop/Binaries/javaprg.sh stop"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接