def _startsensor(self): """ starts the DySKT sensor """ flags = bits.bitmask_list(_STATE_FLAGS_, self._state) if flags['store'] and flags['nidus'] and not flags['dyskt']: # do we have a password if not self._pwd: pwd = self._getpwd() if pwd is None: self.logwrite("Password entry canceled. Cannot continue", gui.LOG_WARN) return self._pwd = pwd # start the sensor try: self.logwrite("Starting DySKT...", gui.LOG_NOTE) cmdline.service('dysktd', self._pwd) time.sleep(0.5) if not cmdline.dysktrunning(wraith.DYSKTPID): raise RuntimeError('unknown') except RuntimeError as e: self.logwrite("Error starting DySKT: %s" % e, gui.LOG_ERR) else: self.logwrite("DySKT Started") self._setstate(_STATE_DYSKT_)
def _chkdyskt(self): self._sv.set("Checking DySKT...") self._pb.step(2.0) if not cmdline.dysktrunning(wraith.DYSKTPID): self._sv.set("Starting DySKT") if not startpsql(pwd): # because DySKT can sometimes take a while, we will run # this several times i = 5 while not cmdline.dysktrunning(wraith.DYSKTPID): i -= 1 if i == 0: break time.sleep(0.5) if cmdline.dysktrunning(wraith.DYSKTPID): self._sv.set("DySKT started") else: self._sv.set("Failed to start DySKT") else: self._sv.set("DySKT already running") self._bfinished = True
def startdyskt(pwd): """ start dyskt sensor pwd: the sudo password """ try: cmdline.service('dysktd',pwd) time.sleep(1.0) if not cmdline.dysktrunning(wraith.DYSKTPID): raise RuntimeError except RuntimeError: return False else: return True
def _updatestate(self): """ reevaluates internal state """ # state of nidus if cmdline.nidusrunning(wraith.NIDUSPID): self._setstate(_STATE_NIDUS_) else: self._setstate(_STATE_NIDUS_,False) # state of dyskt if cmdline.dysktrunning(wraith.DYSKTPID): self._setstate(_STATE_DYSKT_) else: self._setstate(_STATE_DYSKT_,False) # state of postgres i.e. store if cmdline.runningprocess('postgres'): self._setstate(_STATE_STORE_) else: self._setstate(_STATE_STORE_,False) # state of our connection - should figure out a way to determine if # connection is still 'alive' if self._conn: self._setstate(_STATE_CONN_) else: self._setstate(_STATE_CONN_,False)
def _startsensor(self): """ starts the DySKT sensor """ flags = bits.bitmask_list(_STATE_FLAGS_,self._state) if flags['store'] and flags['nidus'] and not flags['dyskt']: # do we have a password if not self._pwd: pwd = self._getpwd() if pwd is None: self.logwrite("Password entry canceled. Cannot continue", gui.LOG_WARN) return self._pwd = pwd # start the sensor try: self.logwrite("Starting DySKT...",gui.LOG_NOTE) cmdline.service('dysktd',self._pwd) time.sleep(0.5) if not cmdline.dysktrunning(wraith.DYSKTPID): raise RuntimeError('unknown') except RuntimeError as e: self.logwrite("Error starting DySKT: %s" % e,gui.LOG_ERR) else: self.logwrite("DySKT Started") self._setstate(_STATE_DYSKT_)
def _create(self): # read in conf file, exit on error msgs = [(time.strftime('%H:%M:%S'), "Wraith v%s" % wraith.__version__, gui.LOG_NOERR)] self._conf = readconf() if 'err' in self._conf: msgs.append((time.strftime('%H:%M:%S'), "Configuration file is invalid. %s" % self._conf['err'], gui.LOG_ERR)) return # determine if postgresql is running if cmdline.runningprocess('postgres'): # update self,msg and connect msgs.append((time.strftime('%H:%M:%S'), 'PostgreSQL is running', gui.LOG_NOERR)) if not self._pwd: self._bSQL = True self._setstate(_STATE_STORE_) (self._conn,ret) = psqlconnect(self._conf['store']) if not self._conn is None: self._setstate(_STATE_CONN_) else: msgs.append((time.strftime('%H:%M:%S'), "Error connecting to PostgreSQL: %s" % ret, gui.LOG_ERR)) return else: msgs.append((time.strftime('%H:%M:%S'), "PostgreSQL is not running", gui.LOG_WARN)) msgs.append((time.strftime('%H:%M:%S'), "Not connected to database", gui.LOG_WARN)) # nidus running? if cmdline.nidusrunning(wraith.NIDUSPID): msgs.append((time.strftime('%H:%M:%S'), "Nidus is running", gui.LOG_NOERR)) self._setstate(_STATE_NIDUS_) else: msgs.append((time.strftime('%H:%M:%S'), "Nidus is not running", gui.LOG_WARN)) # dyskt running? if cmdline.dysktrunning(wraith.DYSKTPID): msgs.append((time.strftime('%H:%M:%S'), "DySKT is running", gui.LOG_NOERR)) self._setstate(_STATE_DYSKT_) else: msgs.append((time.strftime('%H:%M:%S'), "DySKt is not running", gui.LOG_WARN)) # set initial state to initialized self._setstate(_STATE_INIT_) # adjust menu options accordingly self._menuenable() # show log and write messages self.viewlog() self.getpanel("log",True).delayedwrite(msgs)
if sopts is None and stop == False: sopts = 'gui' if sopts is not None and stop == True: ap.error("Cannot start and stop") # stop services - assumes no gui if stop: # verify pwd has been set i = 2 pwd = getpass.unix_getpass(prompt="Password [for %s]:" % getpass.getuser()) while not cmdline.testsudopwd(pwd) and i > 0: pwd = getpass.unix_getpass(prompt="Incorrect password, try again:") i -= 1 if not pwd: ap.error("Three incorrect password attempts") # stop DySKT, then Nidus, then PostgreSQL sd = sn = sp = True if cmdline.dysktrunning(wraith.DYSKTPID): ret = 'ok' if stopdyskt(pwd) else 'fail' print "Stopping DySKT\t\t\t\t[%s]" % ret else: print "DySKT not running" if cmdline.nidusrunning(wraith.NIDUSPID): ret = 'ok' if stopnidus(pwd) else 'fail' print "Stopping Nidus\t\t\t\t[%s]" % ret else: print "Nidus not running" if cmdline.runningprocess('postgres') and not exclude: ret = 'ok' if stoppsql(pwd) else 'fail' print "Stopping PostgreSQL\t\t\t[%s]" % ret else: print "PostgreSQL not running" sys.exit(0) # start specified services if sopts == 'nogui':
def _initialize(self): """ initialize gui, determine initial state """ # configure panel & write initial message # have to manually enter the desired size, as the menu does not expand # the visibile portion automatically self.tk.wm_geometry("300x1+0+0") self.tk.resizable(0,0) self.logwrite("Wraith v%s" % wraith.__version__) # read in conf file, exit on error confMsg = self._readconf() if confMsg: self.logwrite("Configuration file is invalid. " + confMsg,gui.LOG_ERR) return # determine if postgresql is running if cmdline.runningprocess('postgres'): self.logwrite('PostgreSQL is running',gui.LOG_NOTE) self._bSQL = True # update state self._setstate(_STATE_STORE_) curs = None try: # attempt to connect and set state accordingly self._conn = psql.connect(host=self._conf['store']['host'], dbname=self._conf['store']['db'], user=self._conf['store']['user'], password=self._conf['store']['pwd']) # set to use UTC and enable CONN flag curs = self._conn.cursor() curs.execute("set time zone 'UTC';") self._conn.commit() self._setstate(_STATE_CONN_) self.logwrite("Connected to database",gui.LOG_NOTE) except psql.OperationalError as e: if e.__str__().find('connect') > 0: self.logwrite("PostgreSQL is not running",gui.LOG_WARN) self._setstate(_STATE_STORE_,False) elif e.__str__().find('authentication') > 0: self.logwrite("Authentication string is invalid",gui.LOG_ERR) else: self.logwrite("Unspecified DB error occurred",gui.LOG_ERR) self._conn.rollback() self.logwrite("Not connected to database",gui.LOG_WARN) finally: if curs: curs.close() else: self.logwrite("PostgreSQL is not running",gui.LOG_WARN) self.logwrite("Not connected to database",gui.LOG_WARN) # nidus running? if cmdline.nidusrunning(wraith.NIDUSPID): self.logwrite("Nidus is running") self._setstate(_STATE_NIDUS_) else: self.logwrite("Nidus is not running",gui.LOG_WARN) if cmdline.dysktrunning(wraith.DYSKTPID): self.logwrite("DySKT is running") self._setstate(_STATE_DYSKT_) else: self.logwrite("DySKT is not running",gui.LOG_WARN) # set initial state to initialized self._setstate(_STATE_INIT_) # adjust menu options accordingly self._menuenable()
def _initialize(self): """ initialize gui, determine initial state """ # configure panel & write initial message # have to manually enter the desired size, as the menu does not expand # the visibile portion automatically self.tk.wm_geometry("300x1+0+0") self.tk.resizable(0, 0) self.logwrite("Wraith v%s" % wraith.__version__) # read in conf file, exit on error confMsg = self._readconf() if confMsg: self.logwrite("Configuration file is invalid. " + confMsg, gui.LOG_ERR) return # determine if postgresql is running if cmdline.runningprocess('postgres'): self.logwrite('PostgreSQL is running', gui.LOG_NOTE) self._bSQL = True # update state self._setstate(_STATE_STORE_) curs = None try: # attempt to connect and set state accordingly self._conn = psql.connect(host=self._conf['store']['host'], dbname=self._conf['store']['db'], user=self._conf['store']['user'], password=self._conf['store']['pwd']) # set to use UTC and enable CONN flag curs = self._conn.cursor() curs.execute("set time zone 'UTC';") self._conn.commit() self._setstate(_STATE_CONN_) self.logwrite("Connected to database", gui.LOG_NOTE) except psql.OperationalError as e: if e.__str__().find('connect') > 0: self.logwrite("PostgreSQL is not running", gui.LOG_WARN) self._setstate(_STATE_STORE_, False) elif e.__str__().find('authentication') > 0: self.logwrite("Authentication string is invalid", gui.LOG_ERR) else: self.logwrite("Unspecified DB error occurred", gui.LOG_ERR) self._conn.rollback() self.logwrite("Not connected to database", gui.LOG_WARN) finally: if curs: curs.close() else: self.logwrite("PostgreSQL is not running", gui.LOG_WARN) self.logwrite("Not connected to database", gui.LOG_WARN) # nidus running? if cmdline.nidusrunning(wraith.NIDUSPID): self.logwrite("Nidus is running") self._setstate(_STATE_NIDUS_) else: self.logwrite("Nidus is not running", gui.LOG_WARN) if cmdline.dysktrunning(wraith.DYSKTPID): self.logwrite("DySKT is running") self._setstate(_STATE_DYSKT_) else: self.logwrite("DySKT is not running", gui.LOG_WARN) # set initial state to initialized self._setstate(_STATE_INIT_) # adjust menu options accordingly self._menuenable()