summaryrefslogtreecommitdiffstats
path: root/common/recipes-rest/rest-api/files/node_bmc.py
blob: 14b51e7ecc8e83e03c8e7d57e9d1771dc88d2ef1 (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
#!/usr/bin/env python
#
# Copyright 2015-present Facebook. All Rights Reserved.
#
# This program file 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; version 2 of the License.
#
# 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 in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#


from subprocess import *
import re
from node import node
from pal import *

class bmcNode(node):
    def __init__(self, info = None, actions = None):
        if info == None:
            self.info = {}
        else:
            self.info = info
        if actions == None:
            self.actions = []
        else:
            self.actions = actions

    def getInformation(self):
        # Get Platform Name
        name = pal_get_platform_name()

        # Get BMC Reset Reason
        wdt_counter = Popen('devmem 0x1e785010', \
                            shell=True, stdout=PIPE).stdout.read()
        wdt_counter = int(wdt_counter, 0)

        wdt_counter &= 0xff00

        if wdt_counter:
            por_flag = 0
        else:
            por_flag = 1

        if por_flag:
            reset_reason = "Power ON Reset"
        else:
            reset_reason = "User Initiated Reset or WDT Reset"

        # Get BMC's Up Time
        uptime = Popen('uptime', \
                        shell=True, stdout=PIPE).stdout.read()

        # Get Usage information
        data = Popen('top -b n1', \
                            shell=True, stdout=PIPE).stdout.read()
        adata = data.split('\n')
        mem_usage = adata[0]
        cpu_usage = adata[1]

        # Get OpenBMC version
        version = ""
        data = Popen('cat /etc/issue', \
                            shell=True, stdout=PIPE).stdout.read()
        #Version might start with 'v'(wedge) or 'V'(Yosemite)
        if name == 'Yosemite':
            ver = re.search(r'V([\w\d._-]*)\s', data)
        else:
            ver = re.search(r'v([\w\d._-]*)\s', data)
        if ver:
            version = ver.group(1)


        info = {
            "Description": name + " BMC",
            "Reset Reason": reset_reason,
            "Uptime": uptime,
            "Memory Usage": mem_usage,
            "CPU Usage": cpu_usage,
            "OpenBMC Version": version,
            }

        return info;

def get_node_bmc():
    return bmcNode()
OpenPOWER on IntegriCloud