1. Peter Nixon
  2. icappy

Commits

Peter Nixon  committed 2f73b01

initial checkin

  • Participants
  • Branches default

Comments (0)

Files changed (2)

File .hgignore

  • Ignore whitespace
Empty file added.

File icapclient.py

View file
  • Ignore whitespace
+#! /usr/bin/env python
+"""
+A bare bones ICAP client that can be used to check if an ICAP service is responding to OPTIONS requests
+
+The command line parameters are designed to be compatible with OpenNMS GpMonitor (and hopefully Nagios)
+"""
+
+from twisted.internet import reactor
+from twisted.protocols.basic import LineReceiver
+from twisted.python import log
+import sys
+
+__author__ = "Peter Nixon"
+__copyright__ = "Copyright (C) 2013 Peter Nixon"
+__license__ = "Public Domain"
+__version__ = "1.0"
+
+DEBUG = False
+
+OPTIONS_REQUEST = """OPTIONS icap://localhost/service ICAP/1.0\r
+Host: localhost\r
+Encapsulated: null-body=0\r
+\r
+"""
+
+class IcapProtocol(LineReceiver):
+
+    #def makeConnection(self, transport):
+    #    print transport       
+
+    def connectionLost(self, reason):
+        #print reason
+        self.sendData = False
+
+    def connectionMade(self):
+        #print "connection made"
+        self.delimiter = "\n"
+        self.sendData = True
+        #print self.transport
+        self.sendRequest()
+
+    def lineReceived(self, dline):
+        line = dline.rstrip()
+        if line == "ICAP/1.0 200 OK":
+            self.printSuccess()
+        elif line == "ICAP/1.0 400 Bad Request":
+            self.printFailure()
+        elif line == "ICAP/1.0 500 Server error":
+            self.printFailure()
+        else:
+            print "Hmm server said:", line
+
+    def sendRequest(self):
+        self.transport.write(OPTIONS_REQUEST)
+
+    def printSuccess(self):
+        print "SUCCESS"
+        self.transport.loseConnection()
+
+    def printFailure(self):
+        print "FAILURE"
+        self.transport.loseConnection()
+
+class myProtocolFactory():
+    protocol = IcapProtocol
+
+    def doStart(self):
+        pass
+
+    def startedConnecting(self, connectorInstance):
+        #print connectorInstance
+        pass
+
+    def buildProtocol(self, address):
+        #print address
+        return self.protocol()
+
+    def clientConnectionLost(self, connection, reason):
+        log.msg(connection)
+        log.msg(reason)
+        #print reason
+        #print connection
+        reactor.stop()
+
+    def clientConnectionFailed(self, connection, reason):
+        log.msg(connection)
+        log.msg(reason)
+        print "FAILURE"
+        reactor.stop()
+
+    def doStop(self):
+        pass
+
+
+def main():
+    from optparse import OptionParser
+
+    parser = OptionParser(usage='usage: %prog [options]')
+
+    parser.add_option("-H", "--hostname", dest="hostname", default=None,
+                      help='ICAP Server hostname or IP address (default: None)')
+                      
+    parser.add_option('-p', '--port', dest='port', default=1344, type='int',
+                      help='TCP port to send ICAP request to (default: 1344)')
+
+    parser.add_option('-t', '--timeout', dest='timeout', default=3.0, type='float',
+                      help='Timeout in seconds before failing automatically')
+
+    (options, args) = parser.parse_args()
+
+    if (options.hostname):
+        reactor.connectTCP(options.hostname, options.port, myProtocolFactory(), timeout=options.timeout)
+        reactor.run()
+    else:
+        parser.print_help()
+        sys.exit(1)
+        
+if __name__ == '__main__':
+    if DEBUG: log.startLogging(sys.stdout)
+    main()