Monitoring external jobs

Hudson is useful for monitoring the non-interactive execution of processes, such as cron jobs, procmail, inetd-launched processes. Often those tasks are completely unmonitored (which makes it hard for you to notice when things go wrong), or they send e-mails constantly regardless of the success or failure (which results into the same situation as you'll quickly start ignoring them anyway.) Using Hudson enables you to monitor large number of such tasks with little overhead.

Setting up a project

Create a new job and choose "monitoring an external job".

Monitoring an execution

Once you set up a project, you can monitor an execution by running a command like this:

$ export HUDSON_HOME=http://myserver.acme.org/path/to/hudson/
$ java -jar /path/to/hudson-core.jar "job name" <program arg1 arg2...>

The HUDSON_HOME variable is used to locate the server Hudson is running, so this must be set. You can copy hudson-core.jar to other machines if you want to monitor jobs that are run on a different machine. When you do that, you also need ant.jar for now.

stdout and stderr of the program will be recorded, and the non-0 exit code will be considered as a failure.

Monitoring cron jobs

To monitor a cron job, simply run the above set up from your cron script. To avoid receiving e-mails from cron daemon, you might want to write something like:

HUDSON_HOME=http://myserver.acme.org/path/to/hudson/
0 * * * *     export HUDSON_HOME=$HUDSON_HOME; java -jar hudson-core.jar "backup" backup.sh 2>&1 > /dev/null

Note that you can also move the cron job itself to Hudson by using free-style software project, which would also allow you to manually execute the job outside the scheduld executions.

Submit a run programatically

What hudson-core.jar really does is just execute the specified command and send its result to the server as XML over HTTP. The server accepts any submission as long as it follows the expected XML format. This allows you to write programs that send its result to Hudson, by following the same format.

The format is explained below:

<run>
  <log encoding='hexBinary'>...hex binary encoded console output...</log>
  <result>... integer indicating the error code. 0 is success and everything else is failure</result>
  <duration>... milliseconds it took to execute this run ...</duration>
</run>

The duration element is optional. Console output is hexBinary encoded so that you can pass in any control characters that are otherwise disallowed in XML. Elements must be in this exact order.

The above XML needs to be sent to http://myhost/hudson/job/_jobName_/postBuildResult.

Labels

  Edit Labels
(None)
  1. Sep 06, 2007

    Anonymous says:

    If you get: Exception in thread "/opt/kuttig/Dev1BackupScripts/svnbackup.sh: std...

    If you get:

    ----Exception in thread "/opt/kuttig/Dev1-Backup-Scripts/svn_backup.sh: stdout copier" java.lang.NoClassDefFoundError: org/apache/tools/ant/BuildException
            at hudson.util.StreamCopyThread.run(StreamCopyThread.java:27)
    Exception in thread "/opt/kuttig/Dev1-Backup-Scripts/svn_backup.sh: stderr copier" java.lang.NoClassDefFoundError: org/apache/tools/ant/BuildException
            at hudson.util.StreamCopyThread.run(StreamCopyThread.java:27)

    ----call "-Xbootclasspath/a:/usr/local/apache-ant/lib/ant.jar" to your java call (modify the ant path!). So this may result in:
    export HUDSON_HOME=http://localhost:8080/hudson; java -Xbootclasspath/a:/usr/local/apache-ant/lib/ant.jar -jar hudson-core-1.136.jar "SVN_Backup" /opt/svn/svn_backup.sh

    - zeisss 

    1. Sep 16, 2007

      Kohsuke Kawaguchi says:

      This issue is filed as 802@issue.

      This issue is filed as issue #802.

  2. Mar 29, 2008

    Kevin Ketchum says:

    How about running on a Windows machine. I've created a bat file like this: set H...

    How about running on a Windows machine. I've created a bat file like this:

    set HUDSON_HOME=http://localhost:8080
    java -jar C:\hudson_master\lib\hudson-core-1.200.jar "build_release_12.0" call_build.bat release_12.0

    I get this error (which indicates it cannot file hudsonhome): 

    Exception in thread "main" java.lang.NoClassDefFoundError: hudson/remoting/Callable
            at hudson.Main.getHudsonHome(Main.java:47)
            at hudson.Main.run(Main.java:37)
            at hudson.Main.main(Main.java:29)
    Caused by: java.lang.ClassNotFoundException: hudson.remoting.Callable
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClassInternal(Unknown Source)
            ... 3 more

     I'd think that this should work on a Windows machine as well.

    1. Aug 22

      Drew Cox says:

      I had this too (on Linux though), fixed it by copying the "remoting" jar out of ...

      I had this too (on Linux though), fixed it by copying the "remoting" jar out of the distro and into the same directory as the hudson-core jar.  The husdon-core jar has a long classpath defined in its manifest.mf, I haven't quite figured out what dependencies are required for remote monitoring, as I now have this error:

      Exception in thread "catalog.full.zip: stdout copier" java.lang.StringIndexOutOfBoundsException: String index out of range: -3
          at java.lang.String.charAt(String.java:687)
          at hudson.util.EncodingStream.write(EncodingStream.java:21)
          at java.io.FilterOutputStream.write(FilterOutputStream.java:108)
          at hudson.util.DualOutputStream.write(DualOutputStream.java:29)
          at hudson.util.StreamCopyThread.run(StreamCopyThread.java:28)