Tuesday, July 19, 2011

If you are having issues with licensing your KMS clients because you have too few connected or too many and you want to know on-the-fly how many Windows clients are actually licensed through a given server, this plugin is definitely for you. If the client count is below 25, the plugin returns critical. If the client count is at or above 25, it tells you to relax and have another cup of coffee.


Dependencies:
Your KMS server must be running a telnet server.

Invocation:
To invoke, add this to commands.cfg

define command{
   command_name check_kms
   command_line sh /path/to/check_kms $HOSTADDRESS$ 'username' 'password'
}

Where username and password are an administrative user name and password.

Add this to windows.cfg under your KMS host:

define service{
   use generic-service
   host_name Hostname
   service_description KMS
   check_command check_kms
}

Where Hostname is your KMS host name.

Enjoy!

Download
Reactions:

9 comments:

  1. Justin - thanks for your work on this! I took your script and modified it to add a few additional features:
    1) Usage now conforms to plug-in standard (i.e. uses -H as for hostname, -u for user, etc.)
    2) Returns STATE_UNKNOWN when appropriate.
    3) Variable WARN threshold, depending on what OSes you expect your KMS to activate. If only activating W2K8, the KMS will work fine with as few as 5 nodes active.

    Here it is:
    #!/bin/sh

    # Check that a Windows KMS server is running and handing out licenses

    # Inspired by http://blog.hackersare.us/2011/07/checkkms-nagios-plugin.html

    PROGNAME=`basename $0`
    REVISION=1.0

    # get return codes
    . /usr/local/nagios/libexec/utils.sh

    usage()
    {
    cat <&2
    echo CHECK_KMS `status_msg $STATE_UNKNOWN` - "$@"
    exit $STATE_UNKNOWN
    }

    parse_arguments()
    {
    hostname=""
    while getopts "7hH:u:p:" opt; do
    case "$opt" in
    h) usage;;
    7) wintype=7;;
    H) host=$OPTARG;;
    u) user=$OPTARG;;
    p) pass=$OPTARG;;
    v) print_revision $PROGNAME $REVISION;
    exit 0;;
    esac
    done

    shift `expr $OPTIND - 1`
    [ "$#" -gt 0 ] && die "Unknown arguments \"$@\""
    [ "$host" ] || die "You must specify a host"
    [ "$user" ] || die "You must specify a user"
    [ "$pass" ] || die "You must specify a password"
    }

    check_kms_status()
    {
    export host user pass

    KMS=`{ echo open "$host"
    sleep 5
    echo "$user\r"
    sleep 1
    echo "$pass\r"
    sleep 1
    echo "cscript C:\\windows\\system32\\slmgr.vbs -dlv\r"
    sleep 5
    echo "exit\r"
    } | /usr/bin/telnet`

    ORIG=$KMS
    KMS=`echo "$KMS" | /bin/grep -i "Current" | /usr/bin/awk {'print $3'} | /usr/bin/tr '\r' ' '`

    if [ $KMS -le 24 ]
    then
    if [ $wintype -eq 7 ]
    then
    echo "The current count is $KMS. Can activate W2K8 but NOT Win 7. You might want to force activation on a few machines."
    exit $STATE_WARNING
    else
    echo "The current count is $KMS. Can activate W2K8 but NOT Win 7. If you want this to be a WARNING instead then modify the Service Check to include the -7 switch."
    exit $STATE_OK
    fi

    elif [ $MKS -lt 5 ]
    then
    echo "The current count is $KMS. Needs to be 5+ to activate W2K8 and 25+ for Win 7. You need to force activation on some more machines!"
    exit $STATE_CRITICAL
    elif [ $KMS -gt 24 ]
    then
    echo "The current count is $KMS. Go have another cup of coffee."
    exit $STATE_OK
    fi
    echo "(unknown response: $ORIG)"
    exit $STATE_UNKNOWN
    }

    # translates argument into English
    status_msg()
    {
    case "$1" in
    $STATE_OK) echo "OK";;
    $STATE_WARNING) echo "WARNING";;
    $STATE_CRITICAL) echo "CRITICAL";;
    $STATE_UNKNOWN) echo "UNKNOWN";;
    *) echo "ERROR - bad value ($1)";;
    esac
    }

    main()
    {
    msg=`check_kms_status $host $user $pass $wintype`
    rc=$?

    echo CHECK_KMS `status_msg $rc` - "$msg"
    exit $rc
    }


    parse_arguments "$@"
    main "$@"

    ReplyDelete
  2. Oops. The usage routine got a bit mangled. Here's what was between the EOF markers:

    Checks that a Windows KMS server is running and handing
    out licenses. Requires that the Windows host have a
    working Telnet server.

    Usage: `basename $0` -H host -u 'username' -p 'password' [-7]

    host = hostname or IP of KMS server
    username = Windows login ID on KMS server. Due to the way the
    script consumes backslashes, to specify a domain login you must
    use the format domain\\\\\\username. That's SIX backslashes.
    password = Windows password on KMS server.

    If you specify -7, it indicates that you expect your KMS host
    to activate Windows 7 hosts IN ADDITION to the default expectation
    of activating Windows 2008 hosts. The -7 switch will also change the
    default thresholds for WARN and CRITICAL return codes:

    default: CRITICAL if count < 5 (can't activate anything)
    -7: WARN if activation count < 25 - can't activate Win7)
    CRITICAL if count < 5 (can't activate anything)

    ReplyDelete
  3. Wow anon. You put some time in on this. I'll have to try it out.

    ReplyDelete
  4. You can also check the value using WMI (http://technet.microsoft.com/en-us/library/ff793441.aspx) if you have NSCLient++ installed on the server you can get the counter with the following command :

    check_nrpe -H MyServer -c CheckWMIValue -a "Query=Select * from SoftwareLicensingService" MinWarn=25 MinCrit=5 Check:KMSCurrentCount=KeyManagementServiceCurrentCount

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. I tried that. It didn't work after 3-4 hours of messing with it. For some reason WMI just didn't work for the software licensing components with the KMS servers I ran when I made this plugin. As always with Microsoft, I got vague errors with 10^n solutions to the problem from the internets (I'm not a Microsoft guy) - none of which worked.

    Your suggestion would be a more elegant solution. That didn't work for me and it may not work for others as well. This did. So did Anon's code above (Nicer than mine). This post and the plugin submission are for those in a similar situation.

    ReplyDelete
  7. My spouse and I stumbled over here from a different
    page and thought I should check things out. I like what I see so now i
    am following you. Look forward to looking at your web page again.



    Also visit my weblog; internet schuhe

    ReplyDelete
  8. This post will help the internet viewers for setting up new web site or even a weblog from start
    to end.

    My website: young Entrepreneurs business ideas

    ReplyDelete
  9. Thank you for sharing your info. I truly appreciate your efforts and I am waiting for your further post
    thanks once again.

    Review my homepage: cheapest vacations

    ReplyDelete

Subscribe to RSS Feed Follow me on Twitter!