def _load( self ): """ Load QB pricelist CSV file into database. """ try: self._oLock.acquire() try: # Do we have a new CSV file to import? if not os.access( IMPORT_FILE, os.F_OK ): return dbgMsg( 'loading price list from file [%s]' % IMPORT_FILE ) rgoCategory, rgoItem = self._getObjectsFromFile() dbgMsg( 'found categories-[%d] items-[%s] in file' % ( len( rgoCategory ), len( rgoItem ) ) ) self._addToDatabase( rgoCategory, rgoItem ) self._removeFromDatabase( rgoCategory, rgoItem ) # Backup import file if os.access( '%s.bak' % IMPORT_FILE, os.F_OK ): os.unlink( '%s.bak' % IMPORT_FILE ) os.rename( IMPORT_FILE, '%s.bak' % IMPORT_FILE ) except Exception, e: raise Exception, 'error loading price list into database [%s]' % e finally: self._oLock.release()
def delServer(self, oServer): """ Remove server by id """ try: self._oLock.acquire() try: oServerEntry = self.getServer(bSerial=oServer.getSerial()) if oServerEntry is None: dbgMsg('delete server serial-[%d] failed' % oServer.getSerial()) return False self._libDB.query('DELETE FROM Server WHERE bSerial=%s', oServer.getSerial()) # Detect if we removed Auth flag and remove users from database if oServer.checkHasAuth(): self._libDB.query('DELETE FROM CustUser WHERE bSerial=%s', oServer.getSerial()) dbgMsg('deleted server serial-[%d]' % oServer.getSerial()) return True except Exception, e: raise Exception, 'error while attempting to delete server object [%s]' % e finally: self._oLock.release()
def getSyncHash(self, bSerial): """ Return a binary has of our sync data. Used to test if we should sync (data changed). """ try: oServer = self._dbServerList.getServer(bSerial=bSerial) if oServer is None: dbgMsg('unknown serial') return {} if not oServer.checkHasAuth(): dbgMsg('auth not supported for this serial') return {} rgsType = ['rgsUser'] rgs = {} for sType in rgsType: rgsData = self._getSyncData(sType, bSerial) # return an md5 hash of the object serialized as a string try: import hashlib m = hashlib.md5() except ImportError: import md5 m = md5.new() m.update(pickle.dumps(rgsData)) rgs[sType] = m.hexdigest() return rgs except Exception, e: errMsg( 'error while getting sync hash user list for serial-[%s] [%s]' % (bSerial, e)) return {}
def initCache(self): # Initialize cache (watch dependencies here) libCache = lib.cache.Cache() libCache.set('dbAccessList', db.acl.AccessList()) libCache.set('dbRightList', db.rights.RightList()) libCache.set('dbSessionList', db.session.SessionList()) libCache.set('dbServerList', db.server.ServerList()) libCache.set('dbCameraList', db.camera.CameraList()) libCache.set('dbPriceList', db.pricelist.PriceList()) libCache.set('dbDVSLogList', db.dvslog.DVSLogList()) libCache.set('dbMisc', db.misc.Misc()) libCache.set('libUtil', lib.util.Util()) libCache.set('dbUserList', db.user.UserList()) libCache.set('modKey', modules.key.Key()) libCache.set('modConfigUser', modules.config.user.User()) libCache.set('modConfigServer', modules.config.server.Server()) libCache.set('modConfigCamera', modules.config.camera.Camera()) libCache.set('modConfigPricelist', modules.config.pricelist.PriceList()) libCache.set('modAuth', modules.auth.Auth()) libCache.set('modStats', modules.stats.Stats()) libCache.set('modDvslog', modules.dvslog.DVSLog()) libCache.set('modCrash', modules.crash.Crash()) libCache.set('modTicket', modules.ticket.Ticket()) libCache.set('modCustomer', modules.customer.Customer()) libCache.set('modTigerpaw', modules.tigerpaw.Tigerpaw()) # Hook up Process Control Handlers #libCache.get('modUtil')._controlProcess += self.rgoThread['ProcessControl'].controlProcess dbgMsg('loaded cache objects')
def addCamera(self, bSerial, bCamera): """ Create new camera entry. """ try: self._oLock.acquire() try: # Camera does not exist, so add new failed camera self._libDB.query( 'INSERT INTO Camera (bSerial,bCamera,dTimestamp) VALUES (%s,%s,NOW())', bSerial, bCamera) oCameraEntry = CameraEntry() oCameraEntry.setSerial(bSerial) oCameraEntry.setCamera(bCamera) oCameraEntry.setTimestamp(time.time()) oCameraEntry.setSkip(False) dbgMsg('added camera serial-[%d]' % bSerial) return copy.copy(oCameraEntry) except Exception, e: raise Exception, 'error while attempting to add camera object [%s]' % e finally: self._oLock.release()
def _authUserInternal(self, oUser, sPass): try: # See if we are using the old Mysql hash fUseOldHash = False if len(oUser.getPassword()) == 16: fUseOldHash = True (fSuccess, sHash) = self._libUtil.hashPassword(sPass, fUseOldHash=fUseOldHash) if not fSuccess: return False if oUser.getPassword() == sHash: # If using old password hash, upgrade password hash stored in db on the fly if fUseOldHash: if self._libUtil.upgradePasswordHash(oUser.getID(), sPass): dbgMsg('upgrade password hash for user [%s]' % oUser.getName()) return True return False except Exception, e: errMsg('error while authenticating user [%s]' % e) return False
def _processFailedCameras(self): """ Open a ticket for any camera that has been marked failed more than 1 day. """ try: dbgMsg('processing camera failure tickets') oMatchTest = re.compile( '.*test.*') # Regex to skip any "test" systems # Get Serials from Bugzilla that already have tickets open sQuery = "SELECT version, short_desc FROM bugs WHERE " + \ "product_id=7 AND " + \ "short_desc LIKE 'Camera Down - %' AND " + \ "((bug_status<>'RESOLVED') OR (bug_status='RESOLVED' AND (resolution='REMIND' OR resolution='LATER' OR resolution='WONTFIX')))" rgoSerial = self._getBugzillaSerialsAndCameras(sQuery) bCount = 0 bNow = int(time.time()) for oCamera in self._dbCameraList.getList(): oServer = self._dbServerList.getServer( bSerial=oCamera.getSerial()) if oServer is None: continue if oCamera.checkHasSkip(): continue if oMatchTest.match(oServer.getCategories()): continue if oServer.getMaintenance() == 'no': continue if oCamera.getTimestamp() is not None and ( bNow - oCamera.getTimestamp()) <= CAMERA_FAIL_LIMIT: continue # Make sure we do not already have a ticket open bSerial = oServer.getSerial() bCamera = oCamera.getCamera() fFound = False for o in rgoSerial: if bSerial == o[0] and bCamera == o[1]: fFound = True break if fFound: continue if self._openBug("Camera Down", oServer.getSerial(), oServer.getCompany(), oServer.getName(), bCamera=oCamera.getCamera()): bCount += 1 dbgMsg('opened [%d] camera failure tickets' % bCount) except Exception, e: errMsg('error occurred while processing camera failure tickets') errMsg(e)
def addDVSLogEntry(self, bSerial, bEventID, sData, sTimeStamp): """ Add new DVSLogEntry to the DVSLogEntry List. """ try: self._oLock.acquire() try: self._libDB.query('INSERT INTO DVSLog (bSerial, bEventID, sData, dTimeStamp) VALUES (%s,%s,%s,FROM_UNIXTIME(%s))', bSerial, bEventID, sData, sTimeStamp ) rgoResult = self._libDB.query('SELECT bID FROM DVSLog ORDER BY bID LIMIT 1') bID = rgoResult[0][0] oDVSLogEntry = self.getDVSLogEntry( bID ) dbgMsg('added DVSLogEntry id-[%s]' % bID) return oDVSLogEntry except Exception, e: raise Exception, 'error adding DVSLogEntry [%s]' % e finally: self._oLock.release()
def delCamera(self, oCamera): """ Remove camera by id """ try: self._oLock.acquire() try: oCameraEntry = self.getCamera(oCamera.getSerial(), oCamera.getCamera()) if oCameraEntry is None: dbgMsg('delete camera serial-[%d] camera-[%d] failed' % (oCamera.getSerial(), oCamera.getCamera())) return False self._libDB.query( 'DELETE FROM Camera WHERE bSerial=%s AND bCamera=%s', oCamera.getSerial(), oCamera.getCamera()) dbgMsg('deleted camera serial-[%d] camera-[%d]' % (oCamera.getSerial(), oCamera.getCamera())) return True except Exception, e: raise Exception, 'error while attempting to delete camera object [%s]' % e finally: self._oLock.release()
def setEntry(self, oEntry): """ Update our Entry for this server and user. """ try: self._oLock.acquire() try: bRight = 0 for b in oEntry.getRights(): bRight |= b oAccessEntry = self.getEntry(oEntry.getUser()) if oAccessEntry is not None: self._libDB.query( 'UPDATE ACL SET bRight=%s WHERE bUserID=%s', bRight, oEntry.getUser()) dbgMsg('updated acl user-[%d]' % (oEntry.getUser())) return self._libDB.query( 'INSERT INTO ACL (bUserID,bRight) VALUES (%s,%s)', oEntry.getUser(), bRight) dbgMsg('added acl user-[%d]' % (oEntry.getUser())) except Exception, e: raise Exception, 'error while setting entry [%s]' % e finally: self._oLock.release()
def _removeFromDatabase( self, rgoCategory, rgoItem ): """ Remove old items not found in CSV file. """ try: # Remove old Categories bCount = 0 rgoResult = self._libDB.query( 'SELECT bID, sName FROM PLCategory' ) for oResult in rgoResult: if self._findCategory( rgoCategory, oResult[ 1 ] ) is None: self._libDB.query( 'DELETE FROM PLCategory WHERE sName=%s', oResult[ 1 ] ) self._libDB.query( 'DELETE FROM PLItem WHERE bCategory=%s', oResult[ 0 ] ) bCount += 1 dbgMsg( 'removed [%d] old categories' % bCount ) # Remove old Items bCount = 0 rgoResult = self._libDB.query( 'SELECT bCategory, sName FROM PLItem' ) for oResult in rgoResult: if self._findItem( rgoItem, oResult[ 0 ], oResult[ 1 ] ) is None: self._libDB.query( 'DELETE FROM PLItem WHERE bCategory=%s AND sName=%s', oResult[ 0 ], oResult[ 1 ] ) bCount += 1 dbgMsg( 'removed [%d] old items' % bCount ) except Exception, e: raise Exception, 'error removing old items from database [%s]' % e
def addServer(self, bSerial=None, sSeed=None): """ Create new server entry. """ try: self._oLock.acquire() try: if bSerial is not None: self._libDB.query( 'INSERT INTO Server (bSerial) VALUES (%s)', bSerial) else: # V2 # Find next serial rgoResult = self._libDB.query( 'SELECT bSerial FROM Server ORDER BY bSerial DESC LIMIT 1' ) if len(rgoResult) == 0: raise Exception('cannot find next serial') bSerial = int(rgoResult[0][0]) + 1 self._libDB.query( 'INSERT INTO Server (bSerial,sSeed) VALUES (%s,%s)', bSerial, sSeed) oServerEntry = self.getServer(bSerial=bSerial) dbgMsg('added server serial-[%d]' % bSerial) return oServerEntry except Exception, e: raise Exception, 'error while attempting to add server object [%s]' % e finally: self._oLock.release()
def _removeDeadDVRs( self ): """ Remove video link on video.dividia.net on DVR's that are dead. """ try: dbgMsg( 'removing dead dvrs from video.dividia.net link page' ) for oServer in self._dbServerList.getList(): if not oServer.checkHasCategory( 'video' ): continue if oServer.getInstall() is None: continue bNow = int( time.time() ) if oServer.getTimestamp() is None or bNow - oServer.getTimestamp() > REMOVE_CATEGORY_LIMIT: # Remove this DVR from video.dividia.net page try: self._oLock.acquire() try: rgs = [] for s in oServer.getCategories().split( ',' ): if s == 'video': continue rgs.append( s ) oServer.setCategories( ','.join( rgs ) ) self._dbServerList.setServer( oServer ) except Exception, e: errMsg( 'error saving new categories to server object' ) errMsg( e ) finally: self._oLock.release() except Exception, e: errMsg( 'error occurred while updating video.dividia.net page' ) errMsg( e )
def _sendMissingInstallReminderEmail(self): """ Send an email for any serials that have been installed, but are missing an Install date in the database. Skip "test" systems. """ try: # Only send emails if it is Monday and we haven't already sent them rgoTime = time.localtime() if rgoTime.tm_wday != 0: return bLastRun = 0 try: sLastRun = self._dbMisc.get('bugzilla', 'install-reminder-last') if sLastRun != '0': bLastRun = int( time.mktime(time.strptime(sLastRun, '%Y-%m-%d'))) except: errMsg('error parsing install-reminder-last run time') bNow = int(time.time()) if bNow - bLastRun < 86400: # Already ran within last 24 hours return dbgMsg('sending any missing install date reminder emails') oMatchTest = re.compile( '.*test.*') # Regex to skip any "test" systems bCount = 0 sServerList = '' for oServer in self._dbServerList.getList(): if oServer.getMaintenance() != 'install': continue if oServer.getInstall() is not None: continue if oMatchTest.match(oServer.getCategories()): continue bCount += 1 sServerList += '<tr><td>%d</td><td>%s</td><td>%s</td></tr>' % \ ( oServer.getSerial(), oServer.getCompany(), oServer.getName() ) if bCount > 0: self._sendEmail("Install Reminder", '*****@*****.**', sServerList) dbgMsg( 'sent reminder email on [%d] serials that were missing install dates' % bCount) # Save our last run time self._dbMisc.set('bugzilla', 'install-reminder-last', time.strftime('%Y-%m-%d')) except Exception, e: errMsg( 'error occurred while sending missing install date reminder email' ) errMsg(e)
def _processOfflineServers(self): """ Open a ticket for any server that has been offline more than 1 day. If we are going to open more than 30 tickets at a time, then send a warning email and delay since this means our Internet may be down. """ try: dbgMsg('processing offline servers') oMatchTest = re.compile( '.*test.*') # Regex to skip any "test" systems rgoServer = [] # Array to hold Servers we want to open tickets for # Get Serials from Bugzilla that already have tickets open sQuery = "SELECT version FROM bugs WHERE " + \ "product_id=7 AND " + \ "short_desc LIKE 'Offline - %' AND " + \ "((bug_status<>'RESOLVED') OR (bug_status='RESOLVED' AND (resolution='REMIND' OR resolution='LATER')))" rgbSerial = self._getBugzillaSerials(sQuery) bNow = int(time.time()) for oServer in self._dbServerList.getList(): if oServer.checkHasSkip(): continue if oMatchTest.match(oServer.getCategories()): continue if oServer.getMaintenance() == 'no': continue if oServer.getTimestamp() is not None and ( bNow - oServer.getTimestamp()) <= DVS_OFFLINE_LIMIT: continue # Make sure we do not already have a ticket open if oServer.getSerial() in rgbSerial: continue # Delay opening ticket here, just save it for later. # This way we can check if we have more than 10 systems # reporting down at a time to send an alert email instead rgoServer.append(oServer) # Internet down? if len(rgoServer) > 30: self._sendEmail("Internet Down") rgoServer = [] bCount = 0 for oServer in rgoServer: if self._openBug("Offline", oServer.getSerial(), oServer.getCompany(), oServer.getName()): bCount += 1 dbgMsg('opened [%d] offline server tickets' % bCount) except Exception, e: errMsg('error occurred while processing offline server tickets') errMsg(e)
def _openBug(self, bSerial, sCompany, sName, fLocal, sUser, sTraceback): """ Open new bug in bugzilla. """ try: sType = 'Crash Report' dbgMsg( 'Opening Bug [%s] serial-[%d] company-[%s] name-[%s]' % \ ( sType, bSerial, sCompany, sName ) ) if self._fOfflineDebug: dbgMsg('Skipping since we are in offline debug mode') return True sFileIn = BASE + 'templates/dvs-crash.bug' sFileOut = '/tmp/dvs-crash.bug' sAssignee = '*****@*****.**' # Sanitize input for perl replacements sCompany = sCompany.replace('"', "_") sName = sName.replace('"', "_") sAssignee = sAssignee.replace('"', "_") os.system('cp %s %s' % (sFileIn, sFileOut)) os.system("perl -pi -e \"s/\@\@SERIAL\@\@/%03d/g\" %s" % (bSerial, sFileOut)) os.system("perl -pi -e \"s/\@\@COMPANY\@\@/%s/g\" %s" % (re.escape(sCompany), sFileOut)) os.system("perl -pi -e \"s/\@\@NAME\@\@/%s/g\" %s" % (re.escape(sName), sFileOut)) os.system("perl -pi -e \"s/\@\@LOCAL\@\@/%s/g\" %s" % (re.escape(fLocal), sFileOut)) os.system("perl -pi -e \"s/\@\@USER\@\@/%s/g\" %s" % (re.escape(sUser), sFileOut)) os.system("perl -pi -e \"s/\@\@ASSIGNEE\@\@/%s/g\" %s" % (re.escape(sAssignee), sFileOut)) os.system("perl -pi -e \"s/\@\@TRACEBACK\@\@/%s/g\" %s" % (sTraceback, sFileOut)) os.system("perl -pi -e \"s/@/\\\\\\@/g\" %s" % sFileOut) bStatus = os.system( '/usr/local/bin/bz_webservice_demo.pl --uri http://tickets.dividia.net/xmlrpc.cgi --rememberlogin --login bugzilla --password \'dt!8734\' --create %s 2>/dev/null' % sFileOut) os.unlink(sFileOut) if not os.WIFEXITED(bStatus): return False if os.WEXITSTATUS(bStatus) != 0: return False return True except Exception, e: errMsg('error opening new bug in bugzilla') errMsg(e) return False
def _processWeeklyChecks(self): """ Certain maintenance plans include our techs looks at the cameras once a week. This should automatically open tickets for those systems to help us track if the techs are looking at this information. Also, if they notice a bad camera (focus) they should contact to see if we should go onsite. Included in plan2, plan3, plan4, and plan5. """ try: # Only run this check if it is Monday rgoTime = time.localtime() if rgoTime.tm_wday != 0: return dbgMsg('processing weekly maintenance checks') # List of plans that support weekly checks rgsPlans = ['plan2', 'plan3', 'plan4', 'plan5'] # Get Serials from Bugzilla that already have tickets open sQuery = "SELECT version FROM bugs WHERE " + \ "product_id=7 AND " + \ "short_desc LIKE 'Weekly Check - %' AND " + \ "((bug_status<>'RESOLVED') OR (bug_status='RESOLVED' AND (resolution='REMIND' OR resolution='LATER' OR resolution='WONTFIX')))" rgbSerialOpen = self._getBugzillaSerials(sQuery) # Get Serials from Bugzilla that have tickets opened/closed for today sQuery = "SELECT version FROM bugs WHERE " + \ "product_id=7 AND " + \ "short_desc LIKE 'Weekly Check - %' AND " + \ "DATE(delta_ts)='" + time.strftime( '%Y-%m-%d' ) + "'" rgbSerialToday = self._getBugzillaSerials(sQuery) bCount = 0 for oServer in self._dbServerList.getList(): # See if maintenance plan includes weekly checks if not oServer.getMaintenance() in rgsPlans: continue # Is there an existing ticket open for this server already? if oServer.getSerial() in rgbSerialOpen: continue # Was there a ticket opened for this location already today? # It may be closed, but just skip since we already logged it if oServer.getSerial() in rgbSerialToday: continue if self._openBug("Weekly Check", oServer.getSerial(), oServer.getCompany(), oServer.getName()): bCount += 1 dbgMsg('opened [%d] weekly maintenance check tickets' % bCount) except Exception, e: errMsg( 'error occurred while processing weekly maintenance check tickets' ) errMsg(e)
def _checkSick( self ): """ Check if we can access any DVR's that were previously sick. """ rgoServer = [] for oServer in self._dbServerList.getList(): if oServer.checkIsSick(): rgoServer.append( oServer ) dbgMsg( 'rechecking sick dvrs for misconfigured firewalls (sick)' ) self._check( rgoServer )
def addEntry(self, bUser, rgbRight): """ Add new Entry for this user and add to database. """ try: oEntry = AccessEntry(bUser, rgbRight) self.setEntry(oEntry) dbgMsg('added acl user-[%d]' % (oEntry.getUser())) return copy.copy(oEntry) except Exception, e: raise Exception, 'error while adding entry [%s]' % e
def _sendMaintBillingReminderEmail(self): """ Send an email to accounting to verify that all paying maint customers are marked correctly in Monarch. Additionally, this will allow a check that any customers that are not paying will be removed. This email should be send every 1st day of the month. """ try: # Only send emails if it is the 1st and we haven't already sent them rgoTime = time.localtime() if rgoTime.tm_mday != 1: return bLastRun = 0 try: sLastRun = self._dbMisc.get('bugzilla', 'maint-reminder-last') if sLastRun != '0': bLastRun = int( time.mktime(time.strptime(sLastRun, '%Y-%m-%d'))) except: errMsg('error parsing maint-reminder-last run time') bNow = int(time.time()) if bNow - bLastRun < 86400: # Already ran within last 24 hours return dbgMsg('sending maint billing reminder email') bCount = 0 sServerList = '' for oServer in self._dbServerList.getList(): if oServer.getMaintenance() == 'no' or \ oServer.getMaintenance() == 'free' or \ oServer.getMaintenance() == 'install': continue bCount += 1 sServerList += '<tr><td>%d</td><td>%s</td><td>%s</td><td>%s</td></tr>' % \ ( oServer.getSerial(), oServer.getCompany(), oServer.getName(), oServer.getMaintenance() ) if bCount > 0: self._sendEmail("Maint Billing Reminder", '*****@*****.**', sServerList) dbgMsg( 'sent maintenance billing reminder email on [%d] serials that have a valid maintenance plan' % bCount) # Save our last run time self._dbMisc.set('bugzilla', 'maint-reminder-last', time.strftime('%Y-%m-%d')) except Exception, e: errMsg('error occurred while sending maint billing reminder email') errMsg(e)
def setItemDiscount( self, bItem, bDiscount ): """ Set new distributor discount for item. """ try: bDiscount = float( bDiscount ) self._libDB.query( 'UPDATE PLItem SET bDiscount=%s WHERE bID=%s', bDiscount, bItem ) dbgMsg( 'setting new discount-[%.2f] for item-[%d]' % ( bDiscount, bItem ) ) return True except Exception, e: errMsg( 'error setting new item discount [%s]' % bDiscount ) errMsg( e ) return False
def _loadLastTime( self, sWhich ): """ Load last check time from database """ # Load last check sick time from DB bLast = int( time.time() ) try: sLast = self._dbMisc.get( 'checksick', sWhich ) dbgMsg( 'loading last check %s time [%s]' % ( sWhich, sLast ) ) if sLast != '0': bLast = int( time.mktime( time.strptime( sLast, '%Y-%m-%d %H:%M:%S' ) ) ) except: errMsg( 'error parsing last check %s time' % sWhich ) return bLast
def setDiscount( self, bDiscount ): """ Set default discount percentage. """ try: bDiscount = float( bDiscount ) self._libDB.query( 'UPDATE PLMisc SET sValue=%s WHERE sName=%s', bDiscount, 'discount' ) dbgMsg( 'setting new default discount-[%.2f]' % bDiscount ) return True except Exception, e: errMsg( 'error setting default discount [%s]' % bDiscount ) errMsg( e ) return False
def _processInstallMaintenance(self): """ Check all servers that have maintenance set to "install". If greater than 6 months from install date, send sales a reminder email. """ try: dbgMsg('processing install maintenance reminders') oMatchTest = re.compile( '.*test.*') # Regex to skip any "test" systems # Get Serials from Bugzilla that already have tickets open sQuery = "SELECT version FROM bugs WHERE " + \ "product_id=7 AND " + \ "short_desc LIKE 'Maintenance - %' AND " + \ "((bug_status<>'RESOLVED') OR (bug_status='RESOLVED' AND (resolution='REMIND' OR resolution='LATER' OR resolution='WONTFIX')))" rgbSerial = self._getBugzillaSerials(sQuery) bCount = 0 bNow = int(time.time()) for oServer in self._dbServerList.getList(): # See if maintenance plan == "install" and # system has been installed more than 6 months ago if oServer.getInstall() is None: continue if oServer.getMaintenance() != 'install' or \ ( bNow - oServer.getInstall() ) <= MAINT_REMINDER_LIMIT: continue if oServer.checkHasSkip(): continue if oMatchTest.match(oServer.getCategories()): continue # Make sure we do not already have a ticket open if oServer.getSerial() in rgbSerial: continue if self._openBug("Maintenance Reminder", oServer.getSerial(), oServer.getCompany(), oServer.getName()): bCount += 1 dbgMsg('opened [%d] maintenance reminder tickets' % bCount) except Exception, e: errMsg( 'error occurred while processing install maintenance reminders' ) errMsg(e)
def run( self ): try: self._fRunning = True # sleep a bit and let everything initialize first time.sleep( 10 ) self._postInit() # Load last check times from DB bLastCheckSick = self._loadLastTime( 'sick' ) bLastCheckAll = self._loadLastTime( 'all' ) fHaveSick = self._haveSick() while not self._fStop: # Calculate our next run time bNow = int( time.time() ) bSleep = CHECK_LIMIT - ( bNow - bLastCheckAll ) if fHaveSick: bSleep = RECHECK_LIMIT - ( bNow - bLastCheckSick ) if bSleep > 0: dbgMsg( 'sleeping for [%d] seconds' % bSleep ) while not self._fStop and bSleep > 0: time.sleep( 5 ) bSleep -= 5 if self._fStop: break bNow = int( time.time() ) if bNow - bLastCheckAll >= CHECK_LIMIT: self._checkAll() bLastCheckSick = self._saveLastTime( 'sick' ) bLastCheckAll = self._saveLastTime( 'all' ) # Remove DVR's from video.dividia.net that are dead self._removeDeadDVRs() bNow = int( time.time() ) if bNow - bLastCheckSick >= RECHECK_LIMIT: self._checkSick() bLastCheckSick = self._saveLastTime( 'sick' ) fHaveSick = self._haveSick() self._fRunning = False except Exception, e: self._fRunning = False errMsg( 'encountered an error [%s], terminating' % e )
def _authUserLdap(self, sUser, sPass): """ Authenticate user against LDAP database. """ try: sDN = 'uid=%s,ou=People,dc=dividia,dc=net' % sUser self._oLdapServer = ldap.initialize("ldap://dtechpdc.dividia.net") self._oLdapServer.protocol_version = ldap.VERSION3 self._oLdapServer.simple_bind_s(sDN, sPass) self._oLdapServer.unbind_s() return True except Exception, e: dbgMsg('error authenticating against ldap [%s]' % e) return False
def openTicket(self, bSerial, sSummary, sDesc): """ Open a new Ticket in Bugzilla for this Serial """ try: oServer = self._dbServerList.getServer(bSerial=bSerial) if oServer is None: dbgMsg('unknown serial [%d]' % bSerial) return False return self._openBug('Ticket', bSerial, oServer.getCompany(), oServer.getName(), sSummary, sDesc) except Exception, e: errMsg('error while opening ticket for serial [%d]' % bSerial) errMsg(e) raise Exception, "System error while opening ticket."
def openMaintTicket(self, bSerial): """ Manually open a new maintenance ticket for this serial. """ try: oServer = self._dbServerList.getServer(bSerial=bSerial) if oServer is None: dbgMsg('unknown serial [%d]' % bSerial) return False return self._openBug('On-Site Maintenance', bSerial, oServer.getCompany(), oServer.getName()) except Exception, e: errMsg('error while opening maint ticket for serial [%d]' % bSerial) errMsg(e) raise Exception, "System error while opening maint ticket."
def _addToDatabase( self, rgoCategory, rgoItem ): """ Add new items found in CSV file. """ try: # Add new categories first bCount = 0 for oCategory in rgoCategory: bCategoryOld = oCategory.getID() oResult = self._libDB.query( 'SELECT bID FROM PLCategory WHERE sName=%s', oCategory.getName() ) if len( oResult ) == 0: self._libDB.query( 'INSERT INTO PLCategory (sName) VALUES (%s)', oCategory.getName() ) oResult = self._libDB.query( 'SELECT bID FROM PLCategory WHERE sName=%s', oCategory.getName() ) bCount += 1 bCategoryNew = oResult[ 0 ][ 0 ] oCategory.setID( bCategoryNew ) self._updateItemCategory( rgoItem, bCategoryOld, bCategoryNew ) dbgMsg( 'added [%d] new categories' % bCount ) # Add new items now bCount = 0 for oItem in rgoItem: oResult = self._libDB.query( 'SELECT bID FROM PLItem WHERE bCategory=%s AND sName=%s', oItem.getCategory(), oItem.getName() ) if len( oResult ) == 0: self._libDB.query( 'INSERT INTO PLItem (bCategory,sName,sDescription,bCost,bRetail) VALUES (%s,%s,%s,%s,%s)', oItem.getCategory(), oItem.getName(), oItem.getDescription(), oItem.getCost(), oItem.getRetail() ) oResult = self._libDB.query( 'SELECT bID FROM PLItem WHERE bCategory=%s AND sName=%s', oItem.getCategory(), oItem.getName() ) oItem.setID( oResult[ 0 ][ 0 ] ) bCount += 1 else: self._libDB.query( 'UPDATE PLItem SET sDescription=%s, bCost=%s, bRetail=%s WHERE bCategory=%s AND sName=%s', oItem.getDescription(), oItem.getCost(), oItem.getRetail(), oItem.getCategory(), oItem.getName() ) dbgMsg( 'added [%d] new items' % bCount ) except Exception, e: raise Exception, 'error adding new items to database [%s]' % e
def __init__(self, sName): threading.Thread.__init__(self, name=sName) self._sName = sName self._fRunning = False # Are we actually running? self._fStop = False # Stop requested by parent self._oLock = threading.RLock() self._libDBbug = None self._libDB = None self._dbServerList = None self._dbCameraList = None self._dbMisc = None self._dbDVSLogList = None # Offline debug mode # This is useful if running monarch codebase on test server (not webhost) self._fOfflineDebug = False if os.access('/rda/devel', os.F_OK): dbgMsg('Found /rda/devel repo, so running in offline debug mode') dbgMsg('Skipping emails and opening tickets in Bugzilla') self._fOfflineDebug = True # Make sure we have credentials setup for bugzilla try: if not self._fOfflineDebug: if not os.access('/root/.netrc', os.F_OK): # Attempt to make a new one oFile = open('/root/.netrc', 'w') oFile.write('machine "http://tickets.dividia.net/"\n') oFile.write('login bugzilla\n') oFile.write('password dt!8734\n') oFile.close() if not os.access('/usr/local/bin/bz_webservice_demo.pl', os.F_OK): errMsg( 'bz_webserice_demo.pl utility is not installed, tickets will not get opened.' ) except Exception, e: errMsg( 'Bugzilla credential file is missing and we could not create it, tickets will not get opened.' ) errMsg(e)