def run_monitor(): print("Starting Monitor...") sys.stdout.flush() import monitor sys.argv = ['monitor.py', "-vH", "--config={}".format(CONFIG)] monitor.main() print("Monitor stopped.") sys.stdout.flush()
def test_main(server,parser,body): argv=None logger = logging.getLogger('monitor') logger.setLevel(logging.ERROR) with mock.patch.object(logger,'error') as mock_error: monitor.main(argv=None) #server.assert_called_once_with() mock_error.assert_called_once_with('Main loop exited unexpectedly!')
def test_MonitorConfigInterval(self): with self.assertRaises(SystemExit): testargs = ["monitor.py", "-f", "tests/mocks/ini/monitor-nointerval.ini"] with patch.object(sys, 'argv', testargs): monitor.main() with self.assertRaises(SystemExit): testargs = ["monitor.py", "-f", "tests/mocks/ini/monitor-badinterval.ini"] with patch.object(sys, 'argv', testargs): monitor.main()
def test_main_file_not_found(self): with patch('builtins.open') as mock_open: mock_open.side_effect = FileNotFoundError with patch('monitor.sys.exit'): monitor.LogFile = './mylogfile.log' monitor.main() output = sys.stdout.getvalue().strip() self.assertEquals(output, 'Cannot open file :')
def test_main_kbd_interrupt(self, m_open, m_close, m_scan, m_alert, m_redraw): with patch('monitor.time.sleep', side_effect=KeyboardInterrupt): with patch('monitor.sys.exit'): monitor.LogFile = './mylogfile.log' monitor.Cumulative = False monitor.Alert = 20 monitor.Delay = 10 monitor.main() output = sys.stdout.getvalue().strip() self.assertEquals(output, 'Exiting on keyboard interrupt')
def start_service(self): if check_expired(): self.expired_view() return print "**************************************" print self.directoryname.strip() monitor.main(self.directoryname.strip(), self) self.label_status['text'] = 'Service Status: Running' self.label_status['fg'] = GREEN self.button_close['text'] = 'Stop Service' self.unpack_all() self.frame_header.pack() self.label_dont_quit.pack() self.button_close.pack() self.frame_footer.pack()
def run_monitor(): import monitor sys.argv = ['monitor.py', "-vH", "--config={}".format(CONFIG)] monitor.main()
# 登录 @app.route('/login', methods=['POST']) def login(): if request.method == 'POST': name = request.form.get('name') password = request.form.get('password') # 验证表单中提交的用户名和密码 if name == 'admin' and password == 'zym233521': curr_user = User() curr_user.id = name # 通过Flask-Login的login_user方法登录用户 remember=True记住登录 login_user(curr_user, remember=True) else: return render_template('login.html', error="1") return redirect(url_for('index')) # 登出 @app.route('/logout') @login_required def logout(): logout_user() return render_template('login.html') if __name__ == '__main__': monitor.main() print('监视世界') app.run(debug=True, host='0.0.0.0', port='5000')
def main(dbFile, output): monitor.main(dbFile) generate_graphs.main(dbFile, output)
def test_with_reset(self, mock_requests, mock_time): mock_requests.side_effect = get_return_with_reset self.assertTrue(monitor.main())
def setUp(self): from monitor import main app = main({}) from webtest import TestApp self.testapp = TestApp(app)
def main(): global staleTime print 'arguments were', sys.argv, 'pid is', os.getpid() if sys.argv[1] != 'monitor': try: lastPid = open('running.pid', 'r').read() except: lastPid = None open('running.pid', 'w').write(str(os.getpid())) # p = multiprocessing.Process(target=serveForever) # p.start() if sys.argv[1] == 'test': query = '' if len(sys.argv) == 3: query = sys.argv[2] #subprocess.Popen(['python','inject', 'monitor']) mode = 'testing' runInjects(10, 'testing', query) if sys.argv[1] == 'run': query = '' if len(sys.argv) == 4: query = sys.argv[3] #subprocess.Popen(['./warc-inject','monitor']) threads = int(sys.argv[2]) runInjects(threads, 'production', query) print "done running" if len(sys.argv) == 2: if sys.argv[1] == 'monitor': import monitor monitor.main() if sys.argv[1] == 'init': init() print 'initialized' return sys.exit(0) if sys.argv[1] == 'reset': os.unlink('items.db') init() return sys.exit(0) if sys.argv[1] == 'dump': dumpDb() if sys.argv[1] == 'items': showItems() if sys.argv[1] == 'stop': nuke(lastPid) if sys.argv[1] == 'kill': nuke(lastPid, fromOrbit=True) if sys.argv[1] == 'test': subprocess.Popen(['./warc-inject','monitor']) mode = 'testing' runInjects(10, 'testing') if sys.argv[1] == 'migrate': db = getDb() c = db.cursor() c.execute('ALTER TABLE items RENAME TO old') db.commit() c.execute('''CREATE TABLE files (itemId TEXT, fileName TEXT, updated TIMESTAMP, status INTEGER, took FLOAT)''') c.execute('''CREATE TABLE items (item text, checked timestamp)''') c.execute('''CREATE INDEX item_index ON items (item)''') # res = c.execute("select count(*) from old") # print list(res) #res = c.execute("select distinct item from items") alreadyItem = {} # res = c.execute("select * from old") # print (len(list(res))) res = c.execute("select * from old") now = datetime.datetime.now() for (item, fileName, updated, status, took) in res.fetchall(): #print 'inserting row', item if item not in alreadyItem: c.execute("insert INTO items VALUES (?,?)", (item, now)) alreadyItem[item] = c.lastrowid c.execute("INSERT INTO files VALUES (?,?,?,?,?)", (alreadyItem[item], fileName, updated, status, took)) c.execute('''drop table old''') db.commit() db.close() dumpDb() return if sys.argv[1] == 'testsig': def handler(signum, frame): print 'Signal handler called with signal', signum raise IOError("Couldn't open device!") # Set the signal handler and a 5-second alarm signal.signal(signal.SIGTERM, handler) #signal.alarm(5) # This open() may hang indefinitely time.sleep(100) #fd = os.open('/dev/ttyS0', os.O_RDWR) signal.alarm(0) # Disable the alarm # if sys.argv[1] == 'serve': # serveForever() if len(sys.argv) == 3: if sys.argv[1] == 'force': itemName = sys.argv[2] db = getDb() injectItem(itemName, db, 'production') sys.exit(0) if len(sys.argv) == 4: if sys.argv[1] == 'injectfile': staleTime = datetime.timedelta(0,0,0) from multiprocessing.pool import ThreadPool fileName = sys.argv[2] items = filter(lambda x: x, open(fileName, 'r').read().split('\n')) threads = int(sys.argv[3]) pool = ThreadPool(processes=threads) #print zip(files, repeat(getDb(), len(files)), repeat('production', len(files))) def injectItemTupleWrapper(itemName): db = getDb() ret = injectItem(itemName, db, 'production') db.close() return ret answer = pool.map(injectItemTupleWrapper, items) print 'finished: ', answer sys.exit(0) if sys.argv[1] == 'forcefile': staleTime = datetime.timedelta(0,0,0) from multiprocessing.pool import ThreadPool fileName = sys.argv[2] items = filter(lambda x: x, open(fileName, 'r').read().split('\n')) threads = int(sys.argv[3]) pool = ThreadPool(processes=threads) #print zip(files, repeat(getDb(), len(files)), repeat('production', len(files))) def injectItemTupleWrapper(itemName): db = getDb() ret = injectItem(itemName, db, 'force') db.close() return ret answer = pool.map(injectItemTupleWrapper, items) print 'finished: ', answer sys.exit(0) if sys.argv[1] == 'injectitems': from multiprocessing.pool import ThreadPool fileName = sys.argv[2] items = filter(lambda x: x, open(fileName, 'r').read().split('\n')) threads = int(sys.argv[3]) pool = ThreadPool(processes=threads) #print zip(files, repeat(getDb(), len(files)), repeat('production', len(files))) def injectItemTupleWrapper(itemName): db = getDb() ret = injectItem(itemName, db, 'production') db.close() return ret answer = pool.map(injectItemTupleWrapper, items) sys.exit(0)
def main(): global staleTime print 'arguments were', sys.argv, 'pid is', os.getpid() if sys.argv[1] != 'monitor': try: lastPid = open('running.pid', 'r').read() except: lastPid = None open('running.pid', 'w').write(str(os.getpid())) # p = multiprocessing.Process(target=serveForever) # p.start() if sys.argv[1] == 'test': query = '' if len(sys.argv) == 3: query = sys.argv[2] #subprocess.Popen(['python','inject', 'monitor']) mode = 'testing' runInjects(10, 'testing', query) if sys.argv[1] == 'run': query = '' if len(sys.argv) == 4: query = sys.argv[3] #subprocess.Popen(['./warc-inject','monitor']) threads = int(sys.argv[2]) runInjects(threads, 'production', query) print "done running" if len(sys.argv) == 2: if sys.argv[1] == 'monitor': import monitor monitor.main() if sys.argv[1] == 'init': init() print 'initialized' return sys.exit(0) if sys.argv[1] == 'reset': os.unlink('items.db') init() return sys.exit(0) if sys.argv[1] == 'dump': dumpDb() if sys.argv[1] == 'items': showItems() if sys.argv[1] == 'stop': nuke(lastPid) if sys.argv[1] == 'kill': nuke(lastPid, fromOrbit=True) if sys.argv[1] == 'test': subprocess.Popen(['./warc-inject', 'monitor']) mode = 'testing' runInjects(10, 'testing') if sys.argv[1] == 'migrate': db = getDb() c = db.cursor() c.execute('ALTER TABLE items RENAME TO old') db.commit() c.execute('''CREATE TABLE files (itemId TEXT, fileName TEXT, updated TIMESTAMP, status INTEGER, took FLOAT)''' ) c.execute('''CREATE TABLE items (item text, checked timestamp)''') c.execute('''CREATE INDEX item_index ON items (item)''') # res = c.execute("select count(*) from old") # print list(res) #res = c.execute("select distinct item from items") alreadyItem = {} # res = c.execute("select * from old") # print (len(list(res))) res = c.execute("select * from old") now = datetime.datetime.now() for (item, fileName, updated, status, took) in res.fetchall(): #print 'inserting row', item if item not in alreadyItem: c.execute("insert INTO items VALUES (?,?)", (item, now)) alreadyItem[item] = c.lastrowid c.execute("INSERT INTO files VALUES (?,?,?,?,?)", (alreadyItem[item], fileName, updated, status, took)) c.execute('''drop table old''') db.commit() db.close() dumpDb() return if sys.argv[1] == 'testsig': def handler(signum, frame): print 'Signal handler called with signal', signum raise IOError("Couldn't open device!") # Set the signal handler and a 5-second alarm signal.signal(signal.SIGTERM, handler) #signal.alarm(5) # This open() may hang indefinitely time.sleep(100) #fd = os.open('/dev/ttyS0', os.O_RDWR) signal.alarm(0) # Disable the alarm # if sys.argv[1] == 'serve': # serveForever() if len(sys.argv) == 3: if sys.argv[1] == 'force': itemName = sys.argv[2] db = getDb() injectItem(itemName, db, 'production') sys.exit(0) if len(sys.argv) == 4: if sys.argv[1] == 'injectfile': staleTime = datetime.timedelta(0, 0, 0) from multiprocessing.pool import ThreadPool fileName = sys.argv[2] items = filter(lambda x: x, open(fileName, 'r').read().split('\n')) threads = int(sys.argv[3]) pool = ThreadPool(processes=threads) #print zip(files, repeat(getDb(), len(files)), repeat('production', len(files))) def injectItemTupleWrapper(itemName): db = getDb() ret = injectItem(itemName, db, 'production') db.close() return ret answer = pool.map(injectItemTupleWrapper, items) print 'finished: ', answer sys.exit(0) if sys.argv[1] == 'forcefile': staleTime = datetime.timedelta(0, 0, 0) from multiprocessing.pool import ThreadPool fileName = sys.argv[2] items = filter(lambda x: x, open(fileName, 'r').read().split('\n')) threads = int(sys.argv[3]) pool = ThreadPool(processes=threads) #print zip(files, repeat(getDb(), len(files)), repeat('production', len(files))) def injectItemTupleWrapper(itemName): db = getDb() ret = injectItem(itemName, db, 'force') db.close() return ret answer = pool.map(injectItemTupleWrapper, items) print 'finished: ', answer sys.exit(0) if sys.argv[1] == 'injectitems': from multiprocessing.pool import ThreadPool fileName = sys.argv[2] items = filter(lambda x: x, open(fileName, 'r').read().split('\n')) threads = int(sys.argv[3]) pool = ThreadPool(processes=threads) #print zip(files, repeat(getDb(), len(files)), repeat('production', len(files))) def injectItemTupleWrapper(itemName): db = getDb() ret = injectItem(itemName, db, 'production') db.close() return ret answer = pool.map(injectItemTupleWrapper, items) sys.exit(0)