Commit f79853cc authored by Jason Frisvold's avatar Jason Frisvold
Browse files

- Spawn processes

- Check processes before spawning
- New code to check pids
- Some database modifications
parent 40b1e027
......@@ -18,6 +18,7 @@ Reporting
Client - "cloud" component
- Spawn tasks
- Handle concurrent tasks
Server to Server communication - Protocol Buffers??
......
......@@ -31,3 +31,6 @@ datadir = spawner_data
# This defines the name of the sqlite database used for timing data. It will be
# located in the datadir.
timingdb = timing.db
# This defines the location of the nmap binary
nmap_binary = '/usr/local/bin/nmap'
\ No newline at end of file
......@@ -31,6 +31,7 @@ import re
sys.path.append('../libs')
from skynettimer import SkynetTimer
from funcs import pid_exists
# Global Variables
verbose = False
......@@ -91,12 +92,12 @@ def main(argv):
if (len(filelist) > 0):
process_config(timingdb, filelist)
check_complete(timingdb)
spawnlist = check_timing(loopstarttime, timingdb)
if (len(spawnlist) > 0):
spawn_process(timingdb, spawnlist)
check_complete()
# Sleep until the beginning of the next minute
sleeptime = 60 - datetime.utcnow().second
time.sleep(sleeptime)
......@@ -189,13 +190,57 @@ def check_timing(loopstarttime, timingdb):
def spawn_process(timingdb, spawnlist):
logger.info('Spawn Process')
for timer in spawnlist:
print 'Spawning process for timer - ' + str(timer)
try:
timingcursor = timingdb.cursor()
#Popen(['nohup', 'script.sh'], stdout=devnull, stderr=devnull)
except:
e = sys.exc_info()[0]
logger.exception("Error creating cursor : %s", e)
for timer in spawnlist:
start_time = int(time.mktime(time.gmtime()))
logger.debug('Spawning process for timer - ' + str(timer))
spawned = Popen(['nohup', './script-test.sh'])
logger.debug('Spawned - ' + str(spawned.pid))
timingcursor.execute('INSERT INTO spawned (server_id, start_time, pid, '
'overtime) VALUES (?, ?, ?, 0)', (timer,
start_time,
spawned.pid))
timingdb.commit()
def check_complete():
def check_complete(timingdb):
logger.info('Check for completed processes')
try:
timingcursor = timingdb.cursor()
timingcursor.execute('SELECT server_id, start_time, pid FROM spawned')
rows = timingcursor.fetchall()
except:
e = sys.exc_info()[0]
logger.exception("Error getting spawned process data : %s", e)
if (len(rows) > 0):
for row in rows:
logger.debug('Checking id ' + str(row[0]) + ' started at ' +
str(row[1]))
if pid_exists(row[2]):
logger.debug('PID ' + str(row[2]) + ' Still running')
else:
logger.debug('PID ' + str(row[2]) + ' Completed')
end_time = int(time.mktime(time.gmtime()))
timingcursor.execute('DELETE FROM spawned WHERE server_id = ? '
'AND pid = ?', (row[0], row[2]))
timingcursor.execute('INSERT INTO spawn_log (server_id, '
'start_time, end_time) VALUES (?, ?, ?)',
(row[0], row[1], end_time))
timingdb.commit()
def open_timing_database():
logger.info('Opening timing database')
......@@ -219,8 +264,17 @@ def open_timing_database():
)''')
timingcursor.execute('''CREATE TABLE IF NOT EXISTS spawned
( start_time INT,
pid INT
( server_id INT,
start_time INT,
pid INT,
overtime BOOLEAN
)''')
timingcursor.execute('''CREATE TABLE IF NOT EXISTS spawn_log
(server_id INT,
start_time INT,
end_time INT,
status INT
)''')
except:
......
......@@ -87,9 +87,17 @@ CREATE TABLE timers (
)
CREATE TABLE spawned (
id INT,
server_id INT,
start_time INT,
pid INT,
overtime BOOLEAN
)
CREATE TABLE spawn_log (
server_id INT,
start_time INT,
pid INT
end_time INT,
status INT
)
### Control System
......
#!/usr/bin/python
#* Skynet - Automated "Cloud" Security Scanner *#
#* Copyright (C) 2014 Jason Frisvold <friz@godshell.com> *#
#* *#
#* This program is free software; you can redistribute it and/or modify *#
#* it under the terms of the GNU General Public License as published by *#
#* the Free Software Foundation; either version 2 of the License, or *#
#* (at your option) any later version. *#
#* *#
#* This program is distributed in the hope that it will be useful, *#
#* but WITHOUT ANY WARRANTY; without even the implied warranty of *#
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *#
#* GNU General Public License for more details. *#
#* *#
#* You should have received a copy of the GNU General Public License *#
#* along with this program; if not, write to the Free Software *#
#* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *#
import errno
import os
import sys
# Snatched from Stack Overflow :
# https://stackoverflow.com/questions/568271/how-to-check-if-there-exists-a-process-with-a-given-pid
def pid_exists(pid):
"""Check whether pid exists in the current process table.
UNIX only.
"""
if pid < 0:
return False
if pid == 0:
# According to "man 2 kill" PID 0 refers to every process
# in the process group of the calling process.
# On certain systems 0 is a valid PID but we have no way
# to know that in a portable fashion.
raise ValueError('invalid PID 0')
try:
os.kill(pid, 0)
except OSError as err:
if err.errno == errno.ESRCH:
# ESRCH == No such process
return False
elif err.errno == errno.EPERM:
# EPERM clearly means there's a process to deny access to
return True
else:
# According to "man 2 kill" possible error values are
# (EINVAL, EPERM, ESRCH)
raise
else:
return True
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment