<?xml version="1.0"?>

<!DOCTYPE plugin [
  <!ENTITY multi-process-metrics SYSTEM "/pdk/plugins/multi-process-metrics.xml">
]>

<!--
  NOTE: This copyright does *not* cover user programs that use HQ
  program services by normal system calls through the application
  program interfaces provided as part of the Hyperic Plug-in Development
  Kit or the Hyperic Client Development Kit - this is merely considered
  normal use of the program, and does *not* fall under the heading of
  "derived work".
  
  Copyright (C) [2004-2008], Hyperic, Inc.
  This file is part of HQ.
  
  HQ is free software; you can redistribute it and/or modify
  it under the terms version 2 of the GNU General Public License as
  published by the Free Software Foundation. This program is distributed
  in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
  even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  PARTICULAR PURPOSE. See the GNU General Public License for more
  details.
  
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
 -->

<plugin>
  <!-- extracted to: pdk/work/scripts/sendmail/hq-sendmail-stat -->
  <script name="hq-sendmail-stat">
  <![CDATA[
#!/bin/sh

# linux / aix
[ -d "/var/spool/mqueue" ] &&
    msgdir="/var/spool/mqueue" &&
    premsgdir="/var/spool/clientmqueue"

# solaris
[ -d "/usr/spool/mqueue" ] &&
    msgdir="/usr/spool/mqueue" &&
    premsgdir="/usr/spool/clientmqueue"

# If the mqueue dir doesn't exist, exit 1
[ -z "$msgdir" -o ! -r "$msgdir" ] &&
    exit 1

# May not have permission to cd to the mqueue, make sure
# stdout/err don't get echo'd
cd $msgdir > /dev/null 2>&1
[ "$?" != "0" ] &&
    exit 1

# count msgs in sendmail mqueue dir.  DO NOT use find since it
# may fail when there are lots of files 
messfiles=`ls 2>/dev/null | wc -w`

premessfiles=0

if [ ! -z "$premsgdir" ] && [ -d "$premsgdir" ] && [ -r "$premsgdir" ]
then
    [ `cd $premsgdir > /dev/null 2>&1` ] && [ "$?" = "0" ] &&
        premessfiles=`ls 2>/dev/null | wc -w`
fi

echo MessagesInQueue=$messfiles
echo MessagesAwaitingPreprocessing=$premessfiles

exit 0
  ]]>
  </script>

  <server name="Sendmail"
          version="8.x">

    <property name="INVENTORY_ID" value="sendmail"/>
    <!-- hardwire this cosmetic to universal location -->
    <property name="INSTALLPATH" value="/usr/sbin/sendmail"/>

    <config>
      <option name="process.query"
              description="Process Query"
              default="State.Name.eq=sendmail,State.Name.Pne=$1,CredName.User.eq=root"/>
      <option name="exec"
              description="Type &quot;sudo&quot; To Avoid Having Agent As Root"
              default=""/>
    </config>

    <!--notifies the plugin to auto-discover one instance of each service-->
    <property name="HAS_BUILTIN_SERVICES"
              value="true"/>

    <property name="PROC_QUERY"
              value="State.Name.eq=sendmail"/>

    <plugin type="autoinventory"
            class="org.hyperic.hq.product.DaemonDetector"/>

    <plugin type="measurement"
            class="org.hyperic.hq.product.MeasurementPlugin"/>

    <metric name="Availability"
            alias="Availability"
            template="sigar:Type=ProcState,Arg=%process.query%:State"
            category="AVAILABILITY"
            indicator="true"
            units="percentage"
            collectionType="dynamic"/>

    <service name="Message Submission Process">
      <config>
        <option name="user"
                default="smmsp"
                description="Sendmail Message Submission Process User"/>

        <option name="process.query"
                default="State.Name.eq=sendmail,CredName.User.eq=%user%"
                description="PTQL for Sendmail Message Submission Process"/>
      </config>

      <metric name="Availability"
              template="sigar:Type=MultiProcCpu,Arg=%process.query%:Processes"
              indicator="true"/>
      &multi-process-metrics;
    </service>

    <service name="Root Daemon Process">
      <plugin type="autoinventory"/>
      <config>
        <option name="process.query"
                default="State.Name.eq=sendmail,State.Name.Pne=$1,CredName.User.eq=root"
                description="PTQL for Sendmail Root Daemon Process"/>
      </config>
      <metric name="Availability"
              template="sigar:Type=MultiProcCpu,Arg=%process.query%:Processes"
              indicator="true"/>
      &multi-process-metrics;
    </service>

    <!-- sendmail-stat metrics -->
    <filter name="template"
            value="exec:file=pdk/work/scripts/sendmail/hq-sendmail-stat,exec=%exec%:${alias}"/>

    <metric name="Messages In Queue"
            indicator="true"/>

    <metric name="Messages Awaiting Preprocessing"
            indicator="true"/>

    <!-- protocol services+metrics -->
    <service name="SMTP">
      <config>
        <option name="port"
                description="SMTP Post"
                default="25"/>
        <option name="hostname"
                description="SMTP Hostname"
                default="localhost"/>
      </config>
      <filter name="template"
              value="SMTP:hostname=%hostname%,port=%port%:${alias}"/>
      <metric name="Availability"
              indicator="true"/>
      <metric name="Inbound Connections"
              indicator="true"/>
      <metric name="Outbound Connections"
              indicator="true"/>
    </service>
  </server>
  <!-- ==================== Plugin Help =========================== -->
  <help name="Sendmail">
  <![CDATA[
  <p>
  <h3>Configure HQ for monitoring Sendmail</h3>
  </p>
  <p>
  This plugin needs sudo access as root in order to access the appropriate
  <br>
  Sendmail dirs.
  <br>
  To configure sudo (in /etc/sudoers):
  <br>
  Cmnd_Alias HQ_SENDMAIL_STAT = &lt;hqdir&gt;/agent/pdk/work/scripts/sendmail/hq-sendmail-stat
  <br>
  &lt;agentuser&gt; ALL = NOPASSWD: HQ_SENDMAIL_STAT
  </p>
  ]]>
  </help>
  <help name="Sendmail 8.x" include="Sendmail"/>
</plugin>
