<commit_before>#!/usr/bin/env python
#
# Copyright (C) 2013 eNovance SAS <<EMAIL>>
#
# Author: <NAME> <<EMAIL>>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

'''Main entry point for hardware and system detection routines in eDeploy.'''


from commands import getstatusoutput as cmd
import diskinfo
import fcntl
import hpacucli
import infiniband as ib
import megacli
from netaddr import IPNetwork
import os
import pprint
import socket
import struct
import subprocess
import sys
import xml.etree.ElementTree as ET
import re

SIOCGIFNETMASK = 0x891b


def size_in_gb(size):
    'Return the size in GB without the unit.'
    ret = size.replace(' ', '')
    if ret[-2:] == 'GB':
        return ret[:-2]
    elif ret[-2:] == 'TB':
        return ret[:-2] + '000'
    else:
        return ret


def detect_hpa(hw_lst):
    'Detect HP RAID controller configuration.'
    try:
        cli = hpacucli.Cli(debug=False)
        if not cli.launch():
            return False
        controllers = cli.ctrl_all_show()
        if len(controllers) == 0:
            sys.stderr.write("Info: No hpa controller found\n")
            return False

    except hpacucli.Error as expt:
        sys.stderr.write('Info: detect_hpa : %s\n' % expt.value)
        return False

    for controller in controllers:
        try:
            slot = 'slot=%d' % controller[0]
            for _, disks in cli.ctrl_pd_all_show(slot):
                for disk in disks:
                    hw_lst.append(('disk', disk[0], 'type', disk[1]))
                    hw_lst.append(('disk', disk[0], 'slot',
                                   str(controller[0])))
                    hw_lst.append(('disk', disk[0], 'size',
                                   size_in_gb(disk[2])))
        except hpacucli.Error as expt:
            sys.stderr.write('Info: detect_hpa : controller %d : %s\n'
                             % (controller[0], expt.value))

    return True


def detect_megacli(hw_lst):
    'Detect LSI MegaRAID controller configuration.'
    ctrl_num = megacli.adp_count()
    if ctrl_num > 0:
        for ctrl in range(ctrl_num):
            for enc in megacli.enc_info(ctrl):
                for disk_num in range(enc['NumberOfPhysicalDrives']):
                    disk = 'disk%d' % disk_num
                    info = megacli.pdinfo(ctrl,
                                          enc['DeviceId'],
                                          disk_num)
                    hw_lst.append(('pdisk',
                                   disk,
                                   'ctrl',
                                   str(ctrl_num)))
                    hw_lst.append(('pdisk',
                                   disk,
                                   'type',
                                   info['PdType']))
                    hw_lst.append(('pdisk',
                                   disk,
                                   'id',
                                   '%s:%d' % (info['EnclosureDeviceId'],
                                              disk_num)))
                    hw_lst.append(('pdisk',
                                   disk,
                                   'size',
                                   info['CoercedSize'].split()[0]))
                for ld_num in range(megacli.ld_get_num(ctrl)):
                    disk = 'disk%d' % ld_num
                    info = megacli.ld_get_info(ctrl, ld_num)
                    hw_lst.append(('ldisk',
                                   disk,
                                   'current_access_policy',
                                   info['CurrentAccessPolicy']))
                    hw_lst.append(('ldisk',
                                   disk,
                                   'current_cache_policy',
                                   info['CurrentCachePolicy']))
                    hw_lst.append(('ldisk',
                                   disk,
                                   'disk_cache_policy',
                                   info['DiskCachePolicy']))
                    hw_lst.append(('ldisk',
                                   disk,
                                   'name',
                                   info['Name']))
                    hw_lst.append(('ldisk',
                                   disk,
                                   'number_of_drives',
                                   str(info['NumberOfDrives'])))
                    hw_lst.append(('ldisk',
                                   disk,
                                   'raid_level',
                                   info['RaidLevel']))
                    hw_lst.append(('ldisk',
                                   disk,
                                   'sector_size',
                                   str(info['SectorSize'])))
                    hw_lst.append(('ldisk',
                                   disk,
                                   'state',
                                   info['State']))
                    hw_lst.append(('ldisk',
                                   disk,
                                   'Size',
                                   size_in_gb(info['Size'])))
                    hw_lst.append(('ldisk',
                                   disk,
                                   'strip_size',
                                   info['StripSize']))
        return True
    else:
        return False


def detect_disks(hw_lst):
    'Detect disks.'
    names = diskinfo.disknames()
    sizes = diskinfo.disksizes(names)
    for name in [name for name, size in sizes.items() if size > 0]:
        hw_lst.append(('disk', name, 'size', str(sizes[name])))
        item_list = ['vendor', 'model', 'rev']
        for my_item in item_list:
            try:
                with open('/sys/block/%s/device/%s' % (name,
                                                       my_item),
                          'r') as dev:
                    hw_lst.append(('disk', name, my_item,
                                   dev.readline().rstrip('\n').strip()))
            except Exception, excpt:
                sys.stderr.write(
                    'Failed at getting disk information '
                    'at /sys/block/%s/device/%s: %s\n' % (name,
                                                          my_item,
                                                          str(excpt)))

        item_list = ['WCE', 'RCD']<commit_msg>detect: Reporting logical|megaraid|hpa disk's count<commit_after>#!/usr/bin/env python
#
# Copyright (C) 2013 eNovance SAS <<EMAIL>>
#
# Author: <NAME> <<EMAIL>>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

'''Main entry point for hardware and system detection routines in eDeploy.'''


from commands import getstatusoutput as cmd
import diskinfo
import fcntl
import hpacucli
import infiniband as ib
import megacli
from netaddr import IPNetwork
import os
import pprint
import socket
import struct
import subprocess
import sys
import xml.etree.ElementTree as ET
import re

SIOCGIFNETMASK = 0x891b


def size_in_gb(size):
    'Return the size in GB without the unit.'
    ret = size.replace(' ', '')
    if ret[-2:] == 'GB':
        return ret[:-2]
    elif ret[-2:] == 'TB':
        return ret[:-2] + '000'
    else:
        return ret


def detect_hpa(hw_lst):
    'Detect HP RAID controller configuration.'
    disk_count = 0
    try:
        cli = hpacucli.Cli(debug=False)
        if not cli.launch():
            return False
        controllers = cli.ctrl_all_show()
        if len(controllers) == 0:
            sys.stderr.write("Info: No hpa controller found\n")
            return False

    except hpacucli.Error as expt:
        sys.stderr.write('Info: detect_hpa : %s\n' % expt.value)
        return False

    for controller in controllers:
        try:
            slot = 'slot=%d' % controller[0]
            for _, disks in cli.ctrl_pd_all_show(slot):
                for disk in disks:
                    disk_count += 1
                    hw_lst.append(('disk', disk[0], 'type', disk[1]))
                    hw_lst.append(('disk', disk[0], 'slot',
                                   str(controller[0])))
                    hw_lst.append(('disk', disk[0], 'size',
                                   size_in_gb(disk[2])))
        except hpacucli.Error as expt:
            sys.stderr.write('Info: detect_hpa : controller %d : %s\n'
                             % (controller[0], expt.value))

    hw_lst.append(('disk', 'hpa', 'count', disk_count))
    return True


def detect_megacli(hw_lst):
    'Detect LSI MegaRAID controller configuration.'
    ctrl_num = megacli.adp_count()
    disk_count = 0
    if ctrl_num > 0:
        for ctrl in range(ctrl_num):
            for enc in megacli.enc_info(ctrl):
                for disk_num in range(enc['NumberOfPhysicalDrives']):
                    disk_count += 1
                    disk = 'disk%d' % disk_num
                    info = megacli.pdinfo(ctrl,
                                          enc['DeviceId'],
                                          disk_num)
                    hw_lst.append(('pdisk',
                                   disk,
                                   'ctrl',
                                   str(ctrl_num)))
                    hw_lst.append(('pdisk',
                                   disk,
                                   'type',
                                   info['PdType']))
                    hw_lst.append(('pdisk',
                                   disk,
                                   'id',
                                   '%s:%d' % (info['EnclosureDeviceId'],
                                              disk_num)))
                    hw_lst.append(('pdisk',
                                   disk,
                                   'size',
                                   info['CoercedSize'].split()[0]))
                for ld_num in range(megacli.ld_get_num(ctrl)):
                    disk = 'disk%d' % ld_num
                    info = megacli.ld_get_info(ctrl, ld_num)
                    hw_lst.append(('ldisk',
                                   disk,
                                   'current_access_policy',
                                   info['CurrentAccessPolicy']))
                    hw_lst.append(('ldisk',
                                   disk,
                                   'current_cache_policy',
                                   info['CurrentCachePolicy']))
                    hw_lst.append(('ldisk',
                                   disk,
                                   'disk_cache_policy',
                                   info['DiskCachePolicy']))
                    hw_lst.append(('ldisk',
                                   disk,
                                   'name',
                                   info['Name']))
                    hw_lst.append(('ldisk',
                                   disk,
                                   'number_of_drives',
                                   str(info['NumberOfDrives'])))
                    hw_lst.append(('ldisk',
                                   disk,
                                   'raid_level',
                                   info['RaidLevel']))
                    hw_lst.append(('ldisk',
                                   disk,
                                   'sector_size',
                                   str(info['SectorSize'])))
                    hw_lst.append(('ldisk',
                                   disk,
                                   'state',
                                   info['State']))
                    hw_lst.append(('ldisk',
                                   disk,
                                   'Size',
                                   size_in_gb(info['Size'])))
                    hw_lst.append(('ldisk',
                                   disk,
                                   'strip_size',
                                   info['StripSize']))
        hw_lst.append(('disk', 'megaraid', 'count', disk_count))
        return True
    else:
        return False


def detect_disks(hw_lst):
    'Detect disks.'
    names = diskinfo.disknames()
    sizes = diskinfo.disksizes(names)
    disks = [name for name, size in sizes.items() if size > 0]
    hw_lst.append(('disk', 'logical', 'count', len(disks)))
    for name in disks:
        hw_lst.append(('disk', name, 'size', str(sizes[name])))
        item_list = ['vendor', 'model', 'rev']
        for my_item in item_list:
            try:
                with open('/sys/block/%s/device/%s' % (name,
                                                       my_item),
                          'r') as dev:
                    hw_lst.append(('disk', name, my_item,
                                   dev.readline().rstrip('\n').strip()))
            except Exception, excpt:
                sys.stderr.write(
                    'Failed at getting disk information '
                    'at /sys/block/%s/device/%s: %s\n' % (name,
                                                          my_item,
                                                          str(excpt)))

        item_list = ['WCE', 'RCD']
{'fraction_non_alphanumeric': 0.09951405028523136, 'fraction_numerical': 0.004648214663004437, 'mean_word_length': 1.0648538606950706, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 114, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3545795', 'n_tokens_mistral': 3684, 'n_tokens_neox': 3420, 'n_words': 849}