Back to index

nagios-plugins  1.4.16
Public Member Functions | Public Attributes | Static Public Attributes
check_nmap.CheckNmap Class Reference

List of all members.

Public Member Functions

def __init__
def Run
def Version
def param2int_list
def ParseCmdLine
def CheckHost
def CheckOpen
def CheckClosed
def CleanUp

Public Attributes

 tmp_file
 host
 timeout
 debug
 ports
 opt_ports
 ranges
 exit_code
 exit_msg
 active_ports

Static Public Attributes

int UNKNOWN = 1
int OK = 0
int WARNING = 1
int CRITICAL = 2

Detailed Description

Definition at line 74 of file check_nmap.py.


Constructor & Destructor Documentation

def check_nmap.CheckNmap.__init__ (   self,
  cmd_line = [] 
)
Constructor.
   arguments:
    cmd_line: normaly sys.argv[1:] if called as standalone program

Definition at line 84 of file check_nmap.py.

00084 
00085     def __init__(self,cmd_line=[]):
00086         """Constructor.
00087            arguments:
00088             cmd_line: normaly sys.argv[1:] if called as standalone program
00089        """
00090        self.tmp_file=''
00091        self.host=''       # host to check
00092        self.timeout=10    
00093        self.debug=0       # 1= show debug info
00094        self.ports=[]      # list of mandatory ports
00095        self.opt_ports=[]  # list of optional ports
00096        self.ranges=''     # port ranges for nmap
00097        self.exit_code=0   # numerical exit-code
00098        self.exit_msg=''   # message to caller
00099        
00100        self.ParseCmdLine(cmd_line)
       

Member Function Documentation

Definition at line 332 of file check_nmap.py.

00332 
00333     def CheckClosed(self):
00334        closed=''
00335        for p in self.ports:
00336            if p not in self.active_ports:
00337               closed='%s %s' % (closed,p)
00338        return closed
00339 

Definition at line 196 of file check_nmap.py.

00196 
00197     def CheckHost(self):
00198        'Check one host using nmap.'
00199        #
00200        # Create a tmp file for storing nmap output
00201        #
00202        # The tempfile module from python 1.5.2 is stupid
00203        # two processes runing at aprox the same time gets 
00204        # the same tempfile...
00205        # For this reason I use a random suffix for the tmp-file
00206        # Still not 100% safe, but reduces the risk significally
00207        # I also inserted checks at various places, so that
00208        # _if_ two processes in deed get the same tmp-file
00209        # the only result is a normal error message to nagios
00210        #
00211        r=whrandom.whrandom()
00212        self.tmp_file=tempfile.mktemp('.%s')%r.randint(0,100000)
00213        if self.debug:
00214            print 'Tmpfile is: %s'%self.tmp_file
00215        #
00216        # If a range is given, only run nmap on this range
00217        #
00218        if self.ranges<>'':
00219            global nmap_cmd # needed, to avoid error on next line
00220                            # since we assigns to nmap_cmd :)
00221            nmap_cmd='%s -p %s' %(nmap_cmd,self.ranges)  
00222        #
00223        # Prepare a task
00224        #
00225        t=utils.Task('%s %s' %(nmap_cmd,self.host))
00226        #
00227        # Configure a time-out handler
00228        #
00229        th=utils.TimeoutHandler(t.Kill, time_to_live=self.timeout, 
00230                                debug=self.debug)
00231        #
00232        #  Fork of nmap cmd
00233        #
00234        t.Run(detach=0, stdout=self.tmp_file,stderr='/dev/null')
00235        #
00236        # Wait for completition, error or timeout
00237        #
00238        nmap_exit_code=t.Wait(idlefunc=th.Check, interval=1)
00239        #
00240        # Check for timeout
00241        #
00242        if th.WasTimeOut():
00243            self.exit_code=self.CRITICAL
00244            self.exit_msg='CRITICAL - Plugin timed out after %s seconds' % self.timeout
00245            return
00246        #
00247        # Check for exit status of subprocess
00248        # Must do this after check for timeout, since the subprocess
00249        # also returns error if aborted.
00250        #
00251        if nmap_exit_code <> 0:
00252            self.exit_code=self.UNKNOWN
00253            self.exit_msg='nmap program failed with code %s' % nmap_exit_code
00254            return
00255        #
00256        # Read output
00257        #
00258        try:
00259            f = open(self.tmp_file, 'r')
00260            output=f.readlines()
00261            f.close()
00262        except:
00263            self.exit_code=self.UNKNOWN
00264             self.exit_msg='Unable to get output from nmap'
00265            return
00266 
00267        #
00268        # Store open ports in list
00269        #  scans for lines where first word contains '/'
00270        #  and stores part before '/'
00271        #
00272        self.active_ports=[]
00273        try:
00274            for l in output:
00275               if len(l)<2:
00276                   continue
00277               s=string.split(l)[0]
00278               if string.find(s,'/')<1:
00279                   continue
00280               p=string.split(s,'/')[0]
00281               if string.find(l,'open')>1:
00282                   self.active_ports.append(int(p))
00283        except:
00284            # failure due to strange output...
00285            pass
00286 
00287        if self.debug:
00288            print 'Ports found by nmap:   ',self.active_ports
00289        #
00290        # Filter out optional ports, we don't check status for them...
00291        #
00292        try:
00293            for p in self.opt_ports:
00294               self.active_ports.remove(p)
00295            
00296            if self.debug and len(self.opt_ports)>0:
00297               print 'optional ports removed:',self.active_ports
00298        except:
00299            # under extreame loads the remove(p) above failed for me
00300            # a few times, this exception hanlder handles
00301            # this bug-alike situation...
00302            pass
00303 
00304        opened=self.CheckOpen()     
00305        closed=self.CheckClosed()
00306        
00307        if opened <>'':
00308            self.exit_code=self.CRITICAL
00309             self.exit_msg='PORTS CRITICAL - Open:%s Closed:%s'%(opened,closed)
00310        elif closed <>'':
00311            self.exit_code=self.WARNING
00312            self.exit_msg='PORTS WARNING - Closed:%s'%closed
00313        else:
00314            self.exit_code=self.OK
00315            self.exit_msg='PORTS ok - Only defined ports open'
00316     
    

Here is the call graph for this function:

Definition at line 321 of file check_nmap.py.

00321 
00322     def CheckOpen(self):
00323        opened=''
00324        for p in self.active_ports:
00325            if p not in self.ports:
00326               opened='%s %s' %(opened,p)
00327        return opened
       

Definition at line 340 of file check_nmap.py.

00340 
00341     def CleanUp(self):
00342        #
00343        # If temp file exists, get rid of it
00344        #
00345        if self.tmp_file<>'' and os.path.isfile(self.tmp_file):
00346            try:
00347               os.remove(self.tmp_file)
00348            except:
00349               # temp-file colition, some other process already
00350               # removed the same file... 
00351               pass       
00352     
00353        #
00354        # Show numerical exits as string in debug mode
00355        #
00356        if self.debug:
00357            print 'Exitcode:',self.exit_code,
00358            if self.exit_code==self.UNKNOWN:
00359               print 'UNKNOWN'
00360            elif self.exit_code==self.OK:
00361               print 'OK'
00362            elif self.exit_code==self.WARNING:
00363               print 'WARNING'
00364            elif self.exit_code==self.CRITICAL:
00365               print 'CRITICAL'
00366            else:
00367               print 'undefined'
00368        #
00369        # Check if invalid exit code
00370        #
00371        if self.exit_code<-1 or self.exit_code>2:
00372            self.exit_msg=self.exit_msg+' - undefined exit code (%s)' % self.exit_code
00373            self.exit_code=self.UNKNOWN
00374 
00375 
00376         
00377 
00378 
00379 #
00380 # Help texts
#
def check_nmap.CheckNmap.param2int_list (   self,
  s 
)

Definition at line 127 of file check_nmap.py.

00127 
00128     def param2int_list(self,s):
00129        lst=string.split(string.replace(s,',',' '))
00130        try:
00131            for i in range(len(lst)):
00132               lst[i]=int(lst[i])
00133        except:
00134            lst=[]
00135        return lst
           

Here is the caller graph for this function:

def check_nmap.CheckNmap.ParseCmdLine (   self,
  cmd_line 
)

Definition at line 136 of file check_nmap.py.

00136 
00137     def ParseCmdLine(self,cmd_line):
00138        try:
00139            opt_list=getopt(cmd_line,'vH:ho:p:r:t:V',['debug','host=','help',
00140                'optional=','port=','range=','timeout','version'])
00141            for opt in opt_list[0]:
00142               if opt[0]=='-v' or opt[0]=='--debug':
00143                   self.debug=1
00144               elif opt[0]=='-H' or opt[0]=='--host':
00145                   self.host=opt[1]
00146               elif opt[0]=='-h' or opt[0]=='--help':
00147                   doc_help()
00148                   self.exit_code=1 # request termination
00149                   break
00150               elif opt[0]=='-o' or opt[0]=='--optional':
00151                   self.opt_ports=self.param2int_list(opt[1])
00152               elif opt[0]=='-p' or opt[0]=='--port':
00153                   self.ports=self.param2int_list(opt[1])
00154               elif opt[0]=='-r' or opt[0]=='--range':
00155                   r=string.replace(opt[1],':','-')
00156                   self.ranges=r
00157               elif opt[0]=='-t' or opt[0]=='--timeout':
00158                   self.timeout=opt[1]
00159               elif opt[0]=='-V' or opt[0]=='--version':
00160                   print self.Version()
00161                   self.exit_code=1 # request termination
00162                   break
00163               else:
00164                   self.host=''
00165                   break
00166 
00167        except:
00168            # unknown param
00169            self.host=''
00170            
00171        if self.debug:
00172            print 'Params:'
00173            print '-------'
00174            print 'host             = %s' % self.host
00175            print 'timeout          = %s' % self.timeout
00176            print 'ports            = %s' % self.ports
00177            print 'optional ports   = %s' % self.opt_ports
00178            print 'ranges           = %s' % self.ranges
00179            print
00180        
00181        #
00182        # a option that wishes us to terminate now has been given...
00183        # 
00184        # This way, you can test params in debug mode and see what this 
00185        # program recognised by suplying a version param at the end of
00186        # the cmd-line
00187        #
00188        if self.exit_code<>0:
00189            sys.exit(self.UNKNOWN)
00190            
00191        if self.host=='':
00192            doc_syntax()
00193            self.exit_code=self.UNKNOWN
00194            self.exit_msg='UNKNOWN: bad params, try running without any params for syntax'
00195                      

Here is the call graph for this function:

def check_nmap.CheckNmap.Run (   self)
Actually run the process.
   This method should be called exactly once.

Definition at line 101 of file check_nmap.py.

00101 
00102     def Run(self):
00103         """Actually run the process.
00104            This method should be called exactly once.
00105        """
00106        
00107        #
00108        # Only call check_host if cmd line was accepted earlier
00109        #
00110        if self.exit_code==0:
00111            self.CheckHost()
00112 
00113        self.CleanUp()
00114        return self.exit_code,self.exit_msg
    

Definition at line 115 of file check_nmap.py.

00115 
00116     def Version(self):
00117        return 'check_nmap %s' % _version_
    

Here is the caller graph for this function:


Member Data Documentation

Definition at line 271 of file check_nmap.py.

Definition at line 81 of file check_nmap.py.

Definition at line 92 of file check_nmap.py.

Definition at line 96 of file check_nmap.py.

Definition at line 97 of file check_nmap.py.

Definition at line 90 of file check_nmap.py.

int check_nmap.CheckNmap.OK = 0 [static]

Definition at line 79 of file check_nmap.py.

Definition at line 94 of file check_nmap.py.

Definition at line 93 of file check_nmap.py.

Definition at line 95 of file check_nmap.py.

Definition at line 91 of file check_nmap.py.

Definition at line 89 of file check_nmap.py.

int check_nmap.CheckNmap.UNKNOWN = 1 [static]

Definition at line 78 of file check_nmap.py.

int check_nmap.CheckNmap.WARNING = 1 [static]

Definition at line 80 of file check_nmap.py.


The documentation for this class was generated from the following file: