Exemple #1
0
def import_from_config(config_file, config_type=None):
    """
        This will import our config file into a LoPhiConfig or Machine class 
        for each item in the config
        
        This is only done for physical machines.  Virtual machines are handled
        by libvirt.
        
        @param config_file: Config file on disk
        @param config_type: Type of config file to parse
                        controller, machine, sensor
        @return: List of classes of the appropriate type in dictionary 
                referenced by their name
    """
    if config_type is None:
        logger.error("ERROR: Must specify type of config to import.")
        return None

    Config = ConfigParser.ConfigParser()
    Config.read(config_file)

    config_list = {}
    for config in Config.sections():
        name = config

        logging.debug("Intializing config for %s..." % name)

        config = None

        if config_type == "machine":
            # Create our config
            config = MachineConfig(name, Config)

            # What type of machine?
            config_list[name] = PhysicalMachine(config)

        elif config_type == "controller":
            config = ControllerConfig(name, Config)
            config_list[name] = config

        elif config_type == "sensor":
            # These will only be physical sensors
            # Virtual sensors are all derived from the VM name

            sensor_type = int(Config.get(name, "type"))
            if sensor_type == G.SENSOR_TYPES.NETWORK:
                interface = Config.get(name, "interface")
            else:
                sensor_ip = Config.get(name, "ip")
                sensor_port = int(Config.get(name, "port"))

            if sensor_type == G.SENSOR_TYPES.CONTROL:
                config_list[name] = ControlSensorPhysical(sensor_ip,
                                                          sensor_port,
                                                          name=name)
            elif sensor_type == G.SENSOR_TYPES.DISK:
                config_list[name] = DiskSensorPhysical(sensor_ip,
                                                       sensor_port,
                                                       name=name)
            elif sensor_type == G.SENSOR_TYPES.MEMORY:
                config_list[name] = MemorySensorPhysical(sensor_ip,
                                                         sensor_port,
                                                         name=name)
            elif sensor_type == G.SENSOR_TYPES.CPU:
                config_list[name] = CPUSensorPhysical(sensor_ip,
                                                      sensor_port,
                                                      name=name)
            elif sensor_type == G.SENSOR_TYPES.NETWORK:
                config_list[name] = NetworkSensorPhysical(interface, name=name)
            else:
                logging.error("Unrecognized sensor type. (%d)" % sensor_type)

        elif config_type == "images":

            # Setup an empty map
            config_list = {
                G.MACHINE_TYPES.PHYSICAL: {},
                G.MACHINE_TYPES.KVM: {},
                G.MACHINE_TYPES.XEN: {}
            }

            if Config.has_section("physical"):
                for (profile, image) in Config.items("physical"):
                    config_list[G.MACHINE_TYPES.PHYSICAL][
                        profile.lower()] = image

            if Config.has_section("virtual"):
                for (profile, image) in Config.items("virtual"):
                    config_list[G.MACHINE_TYPES.KVM][profile.lower()] = image
                    config_list[G.MACHINE_TYPES.XEN][profile.lower()] = image

        else:
            logging.error("Unknown config file. (%s)" % (config_type))
            return None

        logging.debug(config_list)

    return config_list
def main(options):
    """
        This script will connect to the LO-PHI Disk Sensor and log all of the 
        activity to both a dcap file with RAW data capture
    """

    # Should we automatically set a output dir?
    OUTPUT_DIR = options.output_dir
    if OUTPUT_DIR is None:
        OUTPUT_DIR = "lophi_data_" + datetime.datetime.now().strftime("%m%d")

    # Make sure we can create the output directory
    if not os.path.exists(OUTPUT_DIR):
        try:
            os.makedirs(OUTPUT_DIR)
        except:
            logger.error("Could not create output directory. (%s)" %
                         OUTPUT_DIR)
            return

    # Auto-generate our dcap filename
    log_dcap_filename = os.path.join(
        OUTPUT_DIR, "lophi_disk_" +
        datetime.datetime.now().strftime("%m-%d-%H:%M") + ".dcap")

    print "* Initializing SATA sensor..."

    # Initialize our disk sensor
    if options.sensor_type == G.MACHINE_TYPES.PHYSICAL:
        disk_sensor = DiskSensorPhysical(G.SENSOR_DISK.DEFAULT_IP,
                                         bind_ip=default_dest_ip,
                                         name="SATA_Sensor")

        if not disk_sensor.is_up():
            logger.error("Disk sensor appears to be down.")
            return
    else:
        disk_sensor = DiskSensorVirtual(options.target)

    print "* Logging data to: %s" % log_dcap_filename

    print "* Setting up DCAP logger..."
    # Setup our dcap logger
    # We use a queue so that we don't hold up the socket.
    log_dcap_queue = multiprocessing.Queue()
    log_dcap_writer = CaptureWriter(log_dcap_filename, log_dcap_queue)
    log_dcap_writer.start()

    print "* Connecting to our sensor..."

    # Get data forever and report it back
    disk_sensor._connect()

    if options.sensor_type == G.MACHINE_TYPES.PHYSICAL:
        print "* Enabling SATA extraction..."
        disk_sensor.sata_enable_all()

        print "* Reading SATA Frame packets..."

    else:
        print "* Reading Disk Sensor Packets..."

    UPDATE_INTERVAL = 5  # Seconds
    last_print_time = 0
    while 1:
        try:
            # Get our packet
            # Returns a SATAFrame for physical and DiskSensorPacket for virtual.
            packet = disk_sensor.get_disk_packet()

            # Log to
            if log_dcap_queue is not None:
                log_dcap_queue.put(packet)

            # Should we print something to screen?
            now = time.time()
            if now - last_print_time > UPDATE_INTERVAL:
                size = sizeof_fmt(os.path.getsize(log_dcap_filename))
                print "* Captured %s." % size
                last_print_time = now

        except:
            logger.error("Problem getting disk packet.")
            G.print_traceback()
            break

    if log_dcap_queue is not None:
        log_dcap_writer.stop()

    if options.sensor_type == G.MACHINE_TYPES.PHYSICAL:
        disk_sensor.sata_disable()

    return
Exemple #3
0
def main(options):
    """
        This script will connect to the LO-PHI Disk Sensor and log all of the 
        activity to both a dcap file with RAW data capture
    """
    
    # Should we automatically set a output dir?
    OUTPUT_DIR = options.output_dir
    if OUTPUT_DIR is None:
        OUTPUT_DIR = "lophi_data_"+datetime.datetime.now().strftime("%m%d")
        
    # Make sure we can create the output directory
    if not os.path.exists(OUTPUT_DIR):
        try:
            os.makedirs(OUTPUT_DIR)
        except:
            logger.error("Could not create output directory. (%s)"%OUTPUT_DIR)
            return
    
    # Auto-generate our dcap filename
    log_dcap_filename = os.path.join(OUTPUT_DIR, "lophi_disk_"+datetime.datetime.now().strftime("%m-%d-%H:%M")+".dcap")

    print "* Initializing SATA sensor..."                
    
    # Initialize our disk sensor    
    if options.sensor_type == G.MACHINE_TYPES.PHYSICAL:
        disk_sensor = DiskSensorPhysical(G.SENSOR_DISK.DEFAULT_IP,
                                     bind_ip=default_dest_ip,
                                     name="SATA_Sensor")
        
        if not disk_sensor.is_up():
            logger.error("Disk sensor appears to be down.")
            return
    else:
        disk_sensor = DiskSensorVirtual(options.target)
        
    print "* Logging data to: %s" % log_dcap_filename

    print "* Setting up DCAP logger..."
    # Setup our dcap logger
    # We use a queue so that we don't hold up the socket.
    log_dcap_queue = multiprocessing.Queue()
    log_dcap_writer = CaptureWriter(log_dcap_filename,
                                    log_dcap_queue)
    log_dcap_writer.start()
        
    print "* Connecting to our sensor..."
    
    # Get data forever and report it back
    disk_sensor._connect()

    if options.sensor_type == G.MACHINE_TYPES.PHYSICAL:
        print "* Enabling SATA extraction..."
        disk_sensor.sata_enable_all()
        
        print "* Reading SATA Frame packets..."
        
    else:
        print "* Reading Disk Sensor Packets..."
    
    UPDATE_INTERVAL = 5 # Seconds
    last_print_time = 0
    while 1:
        try:
            # Get our packet
            # Returns a SATAFrame for physical and DiskSensorPacket for virtual.
            packet = disk_sensor.get_disk_packet()    

            # Log to 
            if log_dcap_queue is not None:
                log_dcap_queue.put( packet )     
                
            # Should we print something to screen?
            now = time.time()
            if now - last_print_time > UPDATE_INTERVAL:
                size = sizeof_fmt(os.path.getsize(log_dcap_filename))
                print "* Captured %s."%size
                last_print_time = now
                                
        except:
            logger.error("Problem getting disk packet.")
            G.print_traceback()
            break
            
    if log_dcap_queue is not None:
        log_dcap_writer.stop()
        
    if options.sensor_type == G.MACHINE_TYPES.PHYSICAL:
        disk_sensor.sata_disable()
    
    return
Exemple #4
0
def main(args=None):
    """
        Very simple toy program to interaction with our disk sensor
    """

    opts = optparse.OptionParser()

    opts.add_option(
        "-p",
        "--physicalhost",
        action="store",
        type="string",
        dest="physicalhost",
        default=PHY_HOST,
        help="Physical Host IP (Default: %s)" % PHY_HOST,
    )

    opts.add_option(
        "-o",
        "--operation",
        action="store",
        type="int",
        dest="operation",
        default=0x0,
        help="operation:\n\t0x00 - READ\n\t0x01 - WRITE\n\t0x80 - SATA",
    )

    opts.add_option(
        "-m",
        "--memory_address",
        action="store",
        type="int",
        dest="memory_address",
        default=0,
        help="2 byte memory address (Ex. 0x0000)",
    )

    #    opts.add_option("-l", "--memory_length", action="store", type="int",
    #        dest="memory_length", default=0,
    #        help="number of d-words to write")

    opts.add_option(
        "-d",
        "--data",
        action="store",
        type="string",
        dest="data",
        default="\x00\x00\x00\x00",
        help='String of data that we want to send. (Ex. "\\x00\\x00\\x00\\x01")Make sure to be word aligned!',
    )

    opts.add_option(
        "-l",
        "--length",
        action="store",
        type="int",
        dest="length",
        default=None,
        help="Set the length field. (This will override the automatic calculation based on the data.)",
    )

    opts.add_option(
        "-c", "--count", action="store", type="int", dest="count", default=1, help="Number of times to send command"
    )

    opts.add_option(
        "-P",
        "--print_all_regs",
        action="store",
        type="int",
        dest="print_regs",
        default=1,
        help="Number of times to send command",
    )

    (options, positionals) = opts.parse_args(args)

    """
        Start sensor code
    """

    # What kind of client do we need?
    print "Initalizing Physical client at %s..." % options.physicalhost
    disk_sensor = DiskSensorPhysical(options.physicalhost)

    # Connect to host
    disk_sensor._connect()

    # Decode our data
    data = None
    if options.data is not None:
        data = options.data.decode("string_escape")

    length = options.length

    # Send our command
    for i in range(options.count):
        packet = disk_sensor._send_command(options.operation, options.memory_address, data, length=length)
        print "Response:"
        print packet

    disk_sensor.print_all_registers()

    disk_sensor._disconnect()
Exemple #5
0
def main(args=None):
    """
        Very simple toy program to interaction with our disk sensor
    """

    opts = optparse.OptionParser()

    opts.add_option("-p",
                    "--physicalhost",
                    action="store",
                    type="string",
                    dest="physicalhost",
                    default=PHY_HOST,
                    help="Physical Host IP (Default: %s)" % PHY_HOST)

    opts.add_option(
        "-o",
        "--operation",
        action="store",
        type="int",
        dest="operation",
        default=0x0,
        help="operation:\n\t0x00 - READ\n\t0x01 - WRITE\n\t0x80 - SATA")

    opts.add_option("-m",
                    "--memory_address",
                    action="store",
                    type="int",
                    dest="memory_address",
                    default=0,
                    help="2 byte memory address (Ex. 0x0000)")

    #    opts.add_option("-l", "--memory_length", action="store", type="int",
    #        dest="memory_length", default=0,
    #        help="number of d-words to write")

    opts.add_option(
        "-d",
        "--data",
        action="store",
        type="string",
        dest="data",
        default="\x00\x00\x00\x00",
        help=
        "String of data that we want to send. (Ex. \"\\x00\\x00\\x00\\x01\")Make sure to be word aligned!"
    )

    opts.add_option(
        "-l",
        "--length",
        action="store",
        type="int",
        dest="length",
        default=None,
        help=
        "Set the length field. (This will override the automatic calculation based on the data.)"
    )

    opts.add_option("-c",
                    "--count",
                    action='store',
                    type="int",
                    dest='count',
                    default=1,
                    help="Number of times to send command")

    opts.add_option("-P",
                    "--print_all_regs",
                    action='store',
                    type="int",
                    dest='print_regs',
                    default=1,
                    help="Number of times to send command")

    (options, positionals) = opts.parse_args(args)
    """
        Start sensor code
    """

    # What kind of client do we need?
    print "Initalizing Physical client at %s..." % options.physicalhost
    disk_sensor = DiskSensorPhysical(options.physicalhost)

    # Connect to host
    disk_sensor._connect()

    # Decode our data
    data = None
    if options.data is not None:
        data = options.data.decode('string_escape')

    length = options.length

    # Send our command
    for i in range(options.count):
        packet = disk_sensor._send_command(options.operation,
                                           options.memory_address,
                                           data,
                                           length=length)
        print "Response:"
        print packet

    disk_sensor.print_all_registers()

    disk_sensor._disconnect()