summaryrefslogtreecommitdiffstats
path: root/rteval/sysinfo/osinfo.py
blob: 7eb271acf8e848fe9bbdb81a5dfc32a8a28da54a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# -*- coding: utf-8 -*-
#!/usr/bin/python -tt
#
#   Copyright 2009-2012   Clark Williams <williams@redhat.com>
#   Copyright 2012        David Sommerseth <davids@redhat.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., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#
#   For the avoidance of doubt the "preferred form" of this code is one which
#   is in an open unpatent encumbered format. Where cryptographic key signing
#   forms part of the process of creating an executable the information
#   including keys needed to generate an equivalently functional executable
#   are deemed to be part of the source code.
#

import os, shutil, subprocess, libxml2
from glob import glob
from rteval.Log import Log

class OSInfo(object):
    def __init__(self, logger):
        self.__logger = logger


    def get_base_os(self):
        '''record what userspace we're running on'''
        distro = "unknown"
        for f in ('redhat-release', 'fedora-release'):
            p = os.path.join('/etc', f)
            if os.path.exists(p):
                f = open(p, 'r')
                distro = f.readline().strip()
                f.close()
                break
        return distro


    def copy_dmesg(self, repdir):
        dpath = "/var/log/dmesg"
        if os.path.exists(dpath):
            shutil.copyfile(dpath, os.path.join(repdir, "dmesg"))
            return
        if os.path.exists('/usr/bin/dmesg'):
            subprocess.call('/usr/bin/dmesg > %s' % os.path.join(repdir, "dmesg"), shell=True)
            return
        print "dmesg file not found at %s and no dmesg exe found!" % dpath



    def run_sysreport(self, repdir):
        if os.path.exists('/usr/sbin/sosreport'):
            exe = '/usr/sbin/sosreport'
        elif os.path.exists('/usr/sbin/sysreport'):
            exe = '/usr/sbin/sysreport'
        else:
            raise RuntimeError, "Can't find sosreport/sysreport"

        self.__logger.log(Log.DEBUG, "report tool: %s" % exe)
        options =  ['-k', 'rpm.rpmva=off',
                    '--name=rteval',
                    '--batch']

        self.__logger.log(Log.INFO, "Generating SOS report")
        self.__logger.log(Log.INFO, "using command %s" % " ".join([exe]+options))
        subprocess.call([exe] + options)
        for s in glob('/tmp/s?sreport-rteval-*'):
            self.__logger.log(Log.DEBUG, "moving %s to %s" % (s, repdir))
            shutil.move(s, repdir)


    def MakeReport(self):
        rep_n = libxml2.newNode("uname")

        baseos_n = libxml2.newNode("baseos")
        baseos_n.addContent(self.get_base_os())
        rep_n.addChild(baseos_n)

        (sys, node, release, ver, machine) = os.uname()
        isrt = 1
        if ver.find(' RT ') == -1:
            isrt = 0

        node_n = libxml2.newNode("node")
        node_n.addContent(node)
        rep_n.addChild(node_n)

        arch_n = libxml2.newNode("arch")
        arch_n.addContent(machine)
        rep_n.addChild(arch_n)

        kernel_n = libxml2.newNode("kernel")
        kernel_n.newProp("is_RT", str(isrt))
        kernel_n.addContent(release)
        rep_n.addChild(kernel_n)

        return rep_n



def unit_test(rootdir):
    import sys

    try:
        log = Log()
        log.SetLogVerbosity(Log.DEBUG|Log.INFO)
        osi = OSInfo(logger=log)
        print "Base OS: %s" % osi.get_base_os()

        print "Testing OSInfo::copy_dmesg('/tmp'): ",
        osi.copy_dmesg('/tmp')
        if os.path.isfile("/tmp/dmesg"):
            md5orig = subprocess.check_output(("md5sum","/var/log/dmesg"))
            md5copy = subprocess.check_output(("md5sum","/tmp/dmesg"))
            if md5orig.split(" ")[0] == md5copy.split(" ")[0]:
                print "PASS"
            else:
                print "FAIL (md5sum)"
            os.unlink("/tmp/dmesg")
        else:
            print "FAIL (copy failed)"

        print "Running sysreport/sosreport with output to current dir"
        osi.run_sysreport(".")

        osinfo_xml = osi.MakeReport()
        xml_d = libxml2.newDoc("1.0")
        xml_d.setRootElement(osinfo_xml)
        xml_d.saveFormatFileEnc("-", "UTF-8", 1)

    except Exception, e:
        import traceback
        traceback.print_exc(file=sys.stdout)
        print "** EXCEPTION %s", str(e)
        return 1

if __name__ == '__main__':
    unit_test(None)