/usr/lib/python3/dist-packages/pyghmi/ipmi/command.py is in python3-pyghmi 1.0.32-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 | # vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2013 IBM Corporation
# Copyright 2015-2017 Lenovo
#
# 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.
# This represents the low layer message framing portion of IPMI
from itertools import chain
import pyghmi.constants as const
import pyghmi.exceptions as exc
import pyghmi.ipmi.events as sel
import pyghmi.ipmi.fru as fru
from pyghmi.ipmi.oem.lookup import get_oem_handler
try:
from pyghmi.ipmi.private import session
except ImportError:
session = None
try:
from pyghmi.ipmi.private import localsession
except ImportError:
localsession = None
import pyghmi.ipmi.private.util as pygutil
import pyghmi.ipmi.sdr as sdr
import socket
import struct
try:
range = xrange
except NameError:
pass
try:
buffer
except NameError:
buffer = memoryview
boot_devices = {
'net': 4,
'network': 4,
'pxe': 4,
'hd': 8,
'safe': 0xc,
'cd': 0x14,
'cdrom': 0x14,
'optical': 0x14,
'dvd': 0x14,
'floppy': 0x3c,
'default': 0x0,
'setup': 0x18,
'bios': 0x18,
'f1': 0x18,
1: 'network',
2: 'hd',
3: 'safe',
5: 'optical',
6: 'setup',
15: 'floppy',
0: 'default'
}
power_states = {
"off": 0,
"on": 1,
"reset": 3,
"diag": 4,
"softoff": 5,
"shutdown": 5,
# NOTE(jbjohnso): -1 is not a valid direct boot state,
# but here for convenience of 'in' statement
"boot": -1,
}
def _mask_to_cidr(mask):
maskn = struct.unpack_from('>I', mask)[0]
cidr = 32
while maskn & 0b1 == 0 and cidr > 0:
cidr -= 1
maskn >>= 1
return cidr
def _cidr_to_mask(prefix):
return struct.pack('>I', 2 ** prefix - 1 << (32 - prefix))
class Command(object):
"""Send IPMI commands to BMCs.
This object represents a persistent session to an IPMI device (bmc) and
allows the caller to reuse a single session to issue multiple commands.
This class can be used in a synchronous (wait for answer and return) or
asynchronous fashion (return immediately and provide responses by
callbacks). Synchronous mode is the default behavior.
For asynchronous mode, simply pass in a callback function. It is
recommended to pass in an instance method to callback and ignore the
callback_args parameter. However, callback_args can optionally be populated
if desired.
:param bmc: hostname or ip address of the BMC (default is local)
:param userid: username to use to connect (default to no user)
:param password: password to connect to the BMC (defaults to no password)
:param onlogon: function to run when logon completes in an asynchronous
fashion. This will result in a greenthread behavior.
:param kg: Optional parameter to use if BMC has a particular Kg configured
"""
def __init__(self, bmc=None, userid=None, password=None, port=623,
onlogon=None, kg=None):
# TODO(jbjohnso): accept tuples and lists of each parameter for mass
# operations without pushing the async complexities up the stack
self.onlogon = onlogon
self.bmc = bmc
self._sdr = None
self._oem = None
self._netchannel = None
self._ipv6support = None
self.certverify = None
if bmc is None:
self.ipmi_session = localsession.Session()
elif onlogon is not None:
self.ipmi_session = session.Session(bmc=bmc,
userid=userid,
password=password,
onlogon=self.logged,
port=port,
kg=kg)
# induce one iteration of the loop, now that we would be
# prepared for it in theory
session.Session.wait_for_rsp(0)
else:
self.ipmi_session = session.Session(bmc=bmc,
userid=userid,
password=password,
port=port,
kg=kg)
def register_key_handler(self, callback, type='tls'):
"""Assign a verification handler for a public key
When the library attempts to communicate with the management target
using a non-IPMI protocol, it will try to verify a key. This
allows a caller to register a key handler for accepting or rejecting
a public key/certificate. The callback will be passed the peer public
key or certificate.
:param callback: The function to call with public key/certificate
:param type: Whether the callback is meant to handle 'tls' or 'ssh',
defaults to 'tls'
"""
if type == 'tls':
self.certverify = callback
def logged(self, response):
self.onlogon(response, self)
self.onlogon = None
@classmethod
def eventloop(cls):
while session.Session.wait_for_rsp():
pass
@classmethod
def wait_for_rsp(cls, timeout):
"""Delay for no longer than timeout for next response.
This acts like a sleep that exits on activity.
:param timeout: Maximum number of seconds before returning
"""
return session.Session.wait_for_rsp(timeout=timeout)
def _get_device_id(self):
response = self.raw_command(netfn=0x06, command=0x01)
if 'error' in response:
raise exc.IpmiException(response['error'], code=response['code'])
return {
'device_id': response['data'][0],
'device_revision': response['data'][1] & 0b1111,
'manufacturer_id': struct.unpack(
'<I', struct.pack('3B', *response['data'][6:9]) + b'\x00')[0],
'product_id': struct.unpack(
'<H', struct.pack('2B', *response['data'][9:11]))[0],
}
def oem_init(self):
"""Initialize the command object for OEM capabilities
A number of capabilities are either totally OEM defined or
else augmented somehow by knowledge of the OEM. This
method does an interrogation to identify the OEM.
"""
if self._oem:
return
self._oem = get_oem_handler(self._get_device_id(), self)
def get_bootdev(self):
"""Get current boot device override information.
Provides the current requested boot device. Be aware that not all IPMI
devices support this. Even in BMCs that claim to, occasionally the
BIOS or UEFI fail to honor it. This is usually only applicable to the
next reboot.
:raises: IpmiException on an error.
:returns: dict --The response will be provided in the return as a dict
"""
response = self.raw_command(netfn=0, command=9, data=(5, 0, 0))
# interpret response per 'get system boot options'
if 'error' in response:
raise exc.IpmiException(response['error'])
# this should only be invoked for get system boot option complying to
# ipmi spec and targeting the 'boot flags' parameter
assert (response['command'] == 9 and
response['netfn'] == 1 and
response['data'][0] == 1 and
(response['data'][1] & 0b1111111) == 5)
if (response['data'][1] & 0b10000000 or
not response['data'][2] & 0b10000000):
return {'bootdev': 'default', 'persistent': True}
else: # will consult data2 of the boot flags parameter for the data
persistent = False
uefimode = False
if response['data'][2] & 0b1000000:
persistent = True
if response['data'][2] & 0b100000:
uefimode = True
bootnum = (response['data'][3] & 0b111100) >> 2
bootdev = boot_devices.get(bootnum)
if bootdev:
return {'bootdev': bootdev,
'persistent': persistent,
'uefimode': uefimode}
else:
return {'bootdev': bootnum,
'persistent': persistent,
'uefimode': uefimode}
def reseat_bay(self, bay):
"""Request the reseat of a bay
Request the enclosure manager to reseat the system in a particular
bay.
:param bay: The bay identifier to reseat
:return:
"""
self.oem_init()
self._oem.reseat_bay(bay)
def set_power(self, powerstate, wait=False):
"""Request power state change (helper)
:param powerstate:
* on -- Request system turn on
* off -- Request system turn off without waiting
for OS to shutdown
* shutdown -- Have system request OS proper
shutdown
* reset -- Request system reset without waiting for
OS
* boot -- If system is off, then 'on', else 'reset'
:param wait: If True, do not return until system actually completes
requested state change for 300 seconds.
If a non-zero number, adjust the wait time to the
requested number of seconds
:raises: IpmiException on an error
:returns: dict -- A dict describing the response retrieved
"""
if powerstate not in power_states:
raise exc.InvalidParameterValue(
"Unknown power state %s requested" % powerstate)
newpowerstate = powerstate
response = self.raw_command(netfn=0, command=1)
if 'error' in response:
raise exc.IpmiException(response['error'])
oldpowerstate = 'on' if (response['data'][0] & 1) else 'off'
if oldpowerstate == newpowerstate:
return {'powerstate': oldpowerstate}
if newpowerstate == 'boot':
newpowerstate = 'on' if oldpowerstate == 'off' else 'reset'
response = self.raw_command(
netfn=0, command=2, data=[power_states[newpowerstate]])
if 'error' in response:
raise exc.IpmiException(response['error'])
lastresponse = {'pendingpowerstate': newpowerstate}
waitattempts = 300
if not isinstance(wait, bool):
waitattempts = wait
if (wait and
newpowerstate in ('on', 'off', 'shutdown', 'softoff')):
if newpowerstate in ('softoff', 'shutdown'):
waitpowerstate = 'off'
else:
waitpowerstate = newpowerstate
currpowerstate = None
while currpowerstate != waitpowerstate and waitattempts > 0:
response = self.raw_command(netfn=0, command=1, delay_xmit=1)
if 'error' in response:
raise exc.IpmiException(response['error'])
currpowerstate = 'on' if (response['data'][0] & 1) else 'off'
waitattempts -= 1
if currpowerstate != waitpowerstate:
raise exc.IpmiException(
"System did not accomplish power state change")
return {'powerstate': currpowerstate}
else:
return lastresponse
def get_video_launchdata(self):
"""Get data required to launch a remote video session to target.
This is a highly proprietary scenario, the return data may vary greatly
host to host. The return should be a dict describing the type of data
and the data. For example {'jnlp': jnlpstring}
"""
self.oem_init()
return self._oem.get_video_launchdata()
def reset_bmc(self):
"""Do a cold reset in BMC
"""
response = self.raw_command(netfn=6, command=2)
if 'error' in response:
raise exc.IpmiException(response['error'])
def set_bootdev(self,
bootdev,
persist=False,
uefiboot=False):
"""Set boot device to use on next reboot (helper)
:param bootdev:
*network -- Request network boot
*hd -- Boot from hard drive
*safe -- Boot from hard drive, requesting 'safe mode'
*optical -- boot from CD/DVD/BD drive
*setup -- Boot into setup utility
*default -- remove any IPMI directed boot device
request
:param persist: If true, ask that system firmware use this device
beyond next boot. Be aware many systems do not honor
this
:param uefiboot: If true, request UEFI boot explicitly. Strictly
speaking, the spec sugests that if not set, the system
should BIOS boot and offers no "don't care" option.
In practice, this flag not being set does not preclude
UEFI boot on any system I've encountered.
:raises: IpmiException on an error.
:returns: dict or True -- If callback is not provided, the response
"""
if bootdev not in boot_devices:
return {'error': "Unknown bootdevice %s requested" % bootdev}
bootdevnum = boot_devices[bootdev]
# first, we disable timer by way of set system boot options,
# then move on to set chassis capabilities
# Set System Boot Options is netfn=0, command=8, data
response = self.raw_command(netfn=0, command=8, data=(3, 8))
if 'error' in response:
raise exc.IpmiException(response['error'])
bootflags = 0x80
if uefiboot:
bootflags |= 1 << 5
if persist:
bootflags |= 1 << 6
if bootdevnum == 0:
bootflags = 0
data = (5, bootflags, bootdevnum, 0, 0, 0)
response = self.raw_command(netfn=0, command=8, data=data)
if 'error' in response:
raise exc.IpmiException(response['error'])
return {'bootdev': bootdev}
def xraw_command(self, netfn, command, bridge_request=(), data=(),
delay_xmit=None, retry=True, timeout=None):
"""Send raw ipmi command to BMC, raising exception on error
This is identical to raw_command, except it raises exceptions
on IPMI errors and returns data as a buffer. This is the recommend
function to use. The response['data'] being a buffer allows
traditional indexed access as well as works nicely with
struct.unpack_from when certain data is coming back.
:param netfn: Net function number
:param command: Command value
:param bridge_request: The target slave address and channel number for
the bridge request.
:param data: Command data as a tuple or list
:param retry: Whether to retry this particular payload or not, defaults
to true.
:param timeout: A custom time to wait for initial reply, useful for
a slow command. This may interfere with retry logic.
:returns: dict -- The response from IPMI device
"""
rsp = self.ipmi_session.raw_command(netfn=netfn, command=command,
bridge_request=bridge_request,
data=data, delay_xmit=delay_xmit,
retry=retry, timeout=timeout)
if 'error' in rsp:
raise exc.IpmiException(rsp['error'], rsp['code'])
rsp['data'] = buffer(bytearray(rsp['data']))
return rsp
def raw_command(self, netfn, command, bridge_request=(), data=(),
delay_xmit=None, retry=True, timeout=None):
"""Send raw ipmi command to BMC
This allows arbitrary IPMI bytes to be issued. This is commonly used
for certain vendor specific commands.
Example: ipmicmd.raw_command(netfn=0,command=4,data=(5))
:param netfn: Net function number
:param command: Command value
:param bridge_request: The target slave address and channel number for
the bridge request.
:param data: Command data as a tuple or list
:param retry: Whether or not to retry command if no response received.
Defaults to True
:param timeout: A custom amount of time to wait for initial reply
:returns: dict -- The response from IPMI device
"""
return self.ipmi_session.raw_command(netfn=netfn, command=command,
bridge_request=bridge_request,
data=data, delay_xmit=delay_xmit,
retry=retry, timeout=timeout)
def get_power(self):
"""Get current power state of the managed system
The response, if successful, should contain 'powerstate' key and
either 'on' or 'off' to indicate current state.
:returns: dict -- {'powerstate': value}
"""
response = self.raw_command(netfn=0, command=1)
if 'error' in response:
raise exc.IpmiException(response['error'])
assert (response['command'] == 1 and response['netfn'] == 1)
powerstate = 'on' if (response['data'][0] & 1) else 'off'
return {'powerstate': powerstate}
def set_identify(self, on=True, duration=None):
"""Request identify light
Request the identify light to turn off, on for a duration,
or on indefinitely. Other than error exceptions,
:param on: Set to True to force on or False to force off
:param duration: Set if wanting to request turn on for a duration
rather than indefinitely on
"""
self.oem_init()
try:
self._oem.set_identify(on, duration)
return
except exc.UnsupportedFunctionality:
pass
if duration is not None:
duration = int(duration)
if duration > 255:
duration = 255
if duration < 0:
duration = 0
response = self.raw_command(netfn=0, command=4, data=[duration])
if 'error' in response:
raise exc.IpmiException(response['error'])
return
forceon = 0
if on:
forceon = 1
if self.ipmi_session.ipmiversion < 2.0:
# ipmi 1.5 made due with just one byte, make best effort
# to imitate indefinite as close as possible
identifydata = [255 * forceon]
else:
identifydata = [0, forceon]
response = self.raw_command(netfn=0, command=4, data=identifydata)
if 'error' in response:
raise exc.IpmiException(response['error'])
def init_sdr(self):
"""Initialize SDR
Do the appropriate action to have a relevant sensor description
repository for the current management controller
"""
# For now, return current sdr if it exists and still connected
# future, check SDR timestamp for continued relevance
# further future, optionally support a cache directory/file
# to store cached copies for given device id, product id, mfg id,
# sdr timestamp, our data version revision, aux firmware revision,
# and oem defined field
if self._sdr is None:
self._sdr = sdr.SDR(self)
return self._sdr
def get_event_log(self, clear=False):
"""Retrieve the log of events, optionally clearing
The contents of the SEL are returned as an iterable. Timestamps
are given as local time, ISO 8601 (whether the target has an accurate
clock or not). Timestamps may be omitted for events that cannot be
given a timestamp, leaving only the raw timecode to provide relative
time information. clear set to true will result in the log being
cleared as it is returned. This allows an atomic fetch and clear
behavior so that no log entries will be lost between the fetch and
clear actions. There is no 'clear_event_log' function to encourage
users to create code that is not at risk for losing events.
:param clear: Whether to remove the SEL entries from the target BMC
"""
self.oem_init()
return sel.EventHandler(self.init_sdr(), self).fetch_sel(self, clear)
def decode_pet(self, specifictrap, petdata):
"""Decode PET to an event
In IPMI, the alert format are PET alerts. It is a particular set of
data put into an SNMPv1 trap and sent. It bears no small resemblence
to the SEL entries. This function takes data that would have been
received by an SNMP trap handler, and provides an event decode, similar
to one entry of get_event_log.
:param specifictrap: The specific trap, as either a bytearray or int
:param petdata: An iterable of the octet data of varbind for
1.3.6.1.4.1.3183.1.1.1
:returns: A dict event similar to one iteration of get_event_log
"""
self.oem_init()
return sel.EventHandler(self.init_sdr(), self).decode_pet(specifictrap,
petdata)
def get_inventory_descriptions(self):
"""Retrieve list of things that could be inventoried
This permits a caller to examine the available items
without actually causing the inventory data to be gathered. It
returns an iterable of string descriptions
"""
yield "System"
if self._sdr is None:
self._sdr = sdr.SDR(self)
for fruid in self._sdr.fru:
yield self._sdr.fru[fruid].fru_name
self.oem_init()
for compname in self._oem.get_oem_inventory_descriptions():
yield compname
def get_inventory_of_component(self, component):
"""Retrieve inventory of a component
Retrieve detailed inventory information for only the requested
component.
"""
self.oem_init()
if component == 'System':
return self._get_zero_fru()
if self._sdr is None:
self._sdr = sdr.SDR(self)
for fruid in self._sdr.fru:
if self._sdr.fru[fruid].fru_name == component:
return self._oem.process_fru(fru.FRU(
ipmicmd=self, fruid=fruid, sdr=self._sdr.fru[fruid]).info)
return self._oem.get_inventory_of_component(component)
def _get_zero_fru(self):
# It is expected that a manufacturer matches SMBIOS to IPMI
# get system uuid return data. If a manufacturer does not
# do so, they should handle either deletion or fixup in the
# OEM processing pass. Code optimistically assumes that if
# data is returned, than the vendor is properly using it.
zerofru = fru.FRU(ipmicmd=self).info
if zerofru is None:
zerofru = {}
guiddata = self.raw_command(netfn=6, command=0x37)
if 'error' not in guiddata:
zerofru['UUID'] = pygutil.decode_wireformat_uuid(
guiddata['data'])
# Add some fields returned by get device ID command to FRU 0
# Also rename them to something more in line with FRU 0 field naming
# standards
device_id = self._get_device_id()
device_id['Device ID'] = device_id.pop('device_id')
device_id['Device Revision'] = device_id.pop('device_revision')
device_id['Manufacturer ID'] = device_id.pop('manufacturer_id')
device_id['Product ID'] = device_id.pop('product_id')
zerofru.update(device_id)
return self._oem.process_fru(zerofru)
def get_inventory(self):
"""Retrieve inventory of system
Retrieve inventory of the targeted system. This frequently includes
serial numbers, sometimes hardware addresses, sometimes memory modules
This function will retrieve whatever the underlying platform provides
and apply some structure. Iterating over the return yields tuples
of a name for the inventoried item and dictionary of descriptions
or None for items not present.
"""
self.oem_init()
yield ("System", self._get_zero_fru())
if self._sdr is None:
self._sdr = sdr.SDR(self)
for fruid in self._sdr.fru:
fruinf = fru.FRU(
ipmicmd=self, fruid=fruid, sdr=self._sdr.fru[fruid]).info
if fruinf is not None:
fruinf = self._oem.process_fru(fruinf)
yield (self._sdr.fru[fruid].fru_name, fruinf)
for componentpair in self._oem.get_oem_inventory():
yield componentpair
def get_leds(self):
"""Get LED status information
This provides a detailed view of the LEDs of the managed system.
"""
self.oem_init()
return self._oem.get_leds()
def get_ntp_enabled(self):
self.oem_init()
return self._oem.get_ntp_enabled()
def set_ntp_enabled(self, enable):
self.oem_init()
return self._oem.set_ntp_enabled(enable)
def get_ntp_servers(self):
self.oem_init()
return self._oem.get_ntp_servers()
def set_ntp_server(self, server, index=0):
self.oem_init()
return self._oem.set_ntp_server(server, index)
def get_health(self):
"""Summarize health of managed system
This provides a summary of the health of the managed system.
It additionally provides an iterable list of reasons for
warning, critical, or failed assessments.
"""
summary = {'badreadings': [], 'health': const.Health.Ok}
for reading in self.get_sensor_data():
if reading.health != const.Health.Ok:
summary['health'] |= reading.health
summary['badreadings'].append(reading)
return summary
def get_sensor_reading(self, sensorname):
"""Get a sensor reading by name
Returns a single decoded sensor reading per the name
passed in
:param sensorname: Name of the desired sensor
:returns: sdr.SensorReading object
"""
if self._sdr is None:
self._sdr = sdr.SDR(self)
for sensor in self._sdr.get_sensor_numbers():
if self._sdr.sensors[sensor].name == sensorname:
rsp = self.raw_command(command=0x2d, netfn=4, data=(sensor,))
if 'error' in rsp:
raise exc.IpmiException(rsp['error'], rsp['code'])
return self._sdr.sensors[sensor].decode_sensor_reading(
rsp['data'])
self.oem_init()
return self._oem.get_sensor_reading(sensorname)
def _fetch_lancfg_param(self, channel, param, prefixlen=False):
"""Internal helper for fetching lan cfg parameters
If the parameter revison != 0x11, bail. Further, if 4 bytes, return
string with ipv4. If 6 bytes, colon delimited hex (mac address). If
one byte, return the int value
"""
fetchcmd = bytearray((channel, param, 0, 0))
fetched = self.xraw_command(0xc, 2, data=fetchcmd)
fetchdata = fetched['data']
if ord(fetchdata[0]) != 17:
return None
if len(fetchdata) == 5: # IPv4 address
if prefixlen:
return _mask_to_cidr(fetchdata[1:])
else:
ip = socket.inet_ntoa(fetchdata[1:])
if ip == '0.0.0.0':
return None
return ip
elif len(fetchdata) == 7: # MAC address
mac = '{0:02x}:{1:02x}:{2:02x}:{3:02x}:{4:02x}:{5:02x}'.format(
*bytearray(fetchdata[1:]))
if mac == '00:00:00:00:00:00':
return None
return mac
elif len(fetchdata) == 2:
return ord(fetchdata[1])
else:
raise Exception("Unrecognized data format " + repr(fetchdata))
def set_net_configuration(self, ipv4_address=None, ipv4_configuration=None,
ipv4_gateway=None, channel=None):
"""Set network configuration data.
Apply desired network configuration data, leaving unspecified
parameters alone.
:param ipv4_address: CIDR notation for IP address and netmask
Example: '192.168.0.10/16'
:param ipv4_configuration: Method to use to configure the network.
'DHCP' or 'Static'.
:param ipv4_gateway: IP address of gateway to use.
:param channel: LAN channel to configure, defaults to autodetect
"""
if channel is None:
channel = self.get_network_channel()
if ipv4_configuration is not None:
cmddata = [channel, 4, 0]
if ipv4_configuration.lower() == 'dhcp':
cmddata[-1] = 2
elif ipv4_configuration.lower() == 'static':
cmddata[-1] = 1
else:
raise Exception('Unrecognized ipv4cfg parameter {0}'.format(
ipv4_configuration))
self.xraw_command(netfn=0xc, command=1, data=cmddata)
if ipv4_address is not None:
netmask = None
if '/' in ipv4_address:
ipv4_address, prefix = ipv4_address.split('/')
netmask = _cidr_to_mask(int(prefix))
cmddata = bytearray((channel, 3)) + socket.inet_aton(ipv4_address)
self.xraw_command(netfn=0xc, command=1, data=cmddata)
if netmask is not None:
cmddata = bytearray((channel, 6)) + netmask
self.xraw_command(netfn=0xc, command=1, data=cmddata)
if ipv4_gateway is not None:
cmddata = bytearray((channel, 12)) + socket.inet_aton(ipv4_gateway)
self.xraw_command(netfn=0xc, command=1, data=cmddata)
def get_storage_configuration(self):
""""Get storage configuration data
Retrieves the storage configuration from the target. Data is given
about disks, pools, and volumes. When referencing something, use the
relevant 'cfgpath' attribute to describe it. It is not guaranteed that
cfgpath will be consistent version to version, so a lookup is suggested
in end user applications.
:return: A pyghmi.storage.ConfigSpec object describing current config
"""
self.oem_init()
return self._oem.get_storage_configuration()
def clear_storage_arrays(self):
"""Remove all array and dependent volumes from the system
:return:
"""
self.oem_init()
self._oem.clear_storage_arrays()
def remove_storage_configuration(self, cfgspec):
"""Remove specified storage configuration from controller.
:param cfgspec: A pyghmi.storage.ConfigSpec describing what to remove
:return:
"""
self.oem_init()
return self._oem.remove_storage_configuration(cfgspec)
def apply_storage_configuration(self, cfgspec=None):
"""Evaluate a configuration for validity
This will check if configuration is currently available and, if given,
whether the specified cfgspec can be applied.
:param cfgspec: A pyghmi.storage.ConfigSpec describing desired oonfig
:return:
"""
self.oem_init()
return self._oem.apply_storage_configuration(cfgspec)
def check_storage_configuration(self, cfgspec=None):
"""Evaluate a configuration for validity
This will check if configuration is currently available and, if given,
whether the specified cfgspec can be applied.
:param cfgspec: A pyghmi.storage.ConfigSpec describing desired oonfig
:return:
"""
self.oem_init()
return self._oem.check_storage_configuration(cfgspec)
def get_net_configuration(self, channel=None, gateway_macs=True):
"""Get network configuration data
Retrieve network configuration from the target
:param channel: Channel to configure, defaults to None for 'autodetect'
:param gateway_macs: Whether to retrieve mac addresses for gateways
:returns: A dictionary of network configuration data
"""
if channel is None:
channel = self.get_network_channel()
retdata = {}
v4addr = self._fetch_lancfg_param(channel, 3)
if v4addr is None:
retdata['ipv4_address'] = None
else:
v4masklen = self._fetch_lancfg_param(channel, 6, prefixlen=True)
retdata['ipv4_address'] = '{0}/{1}'.format(v4addr, v4masklen)
v4cfgmethods = {
0: 'Unspecified',
1: 'Static',
2: 'DHCP',
3: 'BIOS',
4: 'Other',
}
retdata['ipv4_configuration'] = v4cfgmethods[self._fetch_lancfg_param(
channel, 4)]
retdata['mac_address'] = self._fetch_lancfg_param(channel, 5)
retdata['ipv4_gateway'] = self._fetch_lancfg_param(channel, 12)
retdata['ipv4_backup_gateway'] = self._fetch_lancfg_param(channel, 14)
if gateway_macs:
retdata['ipv4_gateway_mac'] = self._fetch_lancfg_param(channel, 13)
retdata['ipv4_backup_gateway_mac'] = self._fetch_lancfg_param(
channel, 15)
self.oem_init()
self._oem.add_extra_net_configuration(retdata)
return retdata
def get_sensor_data(self):
"""Get sensor reading objects
Iterates sensor reading objects pertaining to the currently
managed BMC.
:returns: Iterator of sdr.SensorReading objects
"""
if self._sdr is None:
self._sdr = sdr.SDR(self)
for sensor in self._sdr.get_sensor_numbers():
rsp = self.raw_command(command=0x2d, netfn=4, data=(sensor,))
if 'error' in rsp:
if rsp['code'] == 203: # Sensor does not exist, optional dev
continue
raise exc.IpmiException(rsp['error'], code=rsp['code'])
yield self._sdr.sensors[sensor].decode_sensor_reading(rsp['data'])
self.oem_init()
for reading in self._oem.get_sensor_data():
yield reading
def get_sensor_descriptions(self):
"""Get available sensor names
Iterates over the available sensor descriptions
:returns: Iterator of dicts describing each sensor
"""
if self._sdr is None:
self._sdr = sdr.SDR(self)
for sensor in self._sdr.get_sensor_numbers():
yield {'name': self._sdr.sensors[sensor].name,
'type': self._sdr.sensors[sensor].sensor_type}
self.oem_init()
for sensor in self._oem.get_sensor_descriptions():
yield sensor
def get_network_channel(self):
"""Get a reasonable 'default' network channel.
When configuring/examining network configuration, it's desirable to
find the correct channel. Here we run with the 'real' number of the
current channel if it is a LAN channel, otherwise it evaluates
all of the channels to find the first workable LAN channel and returns
that
"""
if self._netchannel is None:
for channel in chain((0xe,), range(1, 0xc)):
try:
rsp = self.xraw_command(
netfn=6, command=0x42, data=(channel,))
except exc.IpmiException as ie:
if ie.ipmicode == 0xcc:
# We have hit an invalid channel, move on to next
# candidate
continue
else:
raise
chantype = ord(rsp['data'][1]) & 0b1111111
if chantype in (4, 6):
try:
# Some implementations denote an inactive channel
# by refusing to do parameter retrieval
if channel != 0xe:
# skip checking if channel is active if we are
# actively using the channel
self.xraw_command(
netfn=0xc, command=2, data=(channel, 5, 0, 0))
# If still here, the channel seems serviceable...
# However some implementations may still have
# ambiguous channel info, that will need to be
# picked up on an OEM extension...
self._netchannel = ord(rsp['data'][0]) & 0b1111
break
except exc.IpmiException as ie:
# This means the attempt to fetch parameter 5 failed,
# therefore move on to next candidate channel
continue
return self._netchannel
def get_alert_destination_count(self, channel=None):
"""Get the number of supported alert destinations
:param channel: Channel for alerts to be examined, defaults to current
"""
if channel is None:
channel = self.get_network_channel()
rqdata = (channel, 0x11, 0, 0)
rsp = self.xraw_command(netfn=0xc, command=2, data=rqdata)
return ord(rsp['data'][1])
def get_alert_destination(self, destination=0, channel=None):
"""Get alert destination
Get a specified alert destination. Returns a dictionary of relevant
configuration. The following keys may be present:
acknowledge_required - Indicates whether the target expects an
acknowledgement
acknowledge_timeout - How long it will wait for an acknowledgment
before retrying
retries - How many attempts will be made to deliver the alert to this
destination
address_format - 'ipv4' or 'ipv6'
address - The IP address of the target
:param destination: The destination number. Defaults to 0
:param channel: The channel for alerting. Defaults to current channel
"""
destinfo = {}
if channel is None:
channel = self.get_network_channel()
rqdata = (channel, 18, destination, 0)
rsp = self.xraw_command(netfn=0xc, command=2, data=rqdata)
dtype, acktimeout, retries = struct.unpack('BBB', rsp['data'][2:])
destinfo['acknowledge_required'] = dtype & 0b10000000 == 0b10000000
# Ignore destination type for now...
if destinfo['acknowledge_required']:
destinfo['acknowledge_timeout'] = acktimeout
destinfo['retries'] = retries
rqdata = (channel, 19, destination, 0)
rsp = self.xraw_command(netfn=0xc, command=2, data=rqdata)
if ord(rsp['data'][2]) & 0b11110000 == 0:
destinfo['address_format'] = 'ipv4'
destinfo['address'] = socket.inet_ntoa(rsp['data'][4:8])
elif ord(rsp['data'][2]) & 0b11110000 == 0b10000:
destinfo['address_format'] = 'ipv6'
destinfo['address'] = socket.inet_ntop(socket.AF_INET6,
rsp['data'][3:])
return destinfo
def clear_alert_destination(self, destination=0, channel=None):
"""Clear an alert destination
Remove the specified alert destination configuration.
:param destination: The destination to clear (defaults to 0)
"""
if channel is None:
channel = self.get_network_channel()
self.set_alert_destination(
'0.0.0.0', False, 0, 0, destination, channel)
def set_alert_community(self, community, channel=None):
"""Set the community string for alerts
This configures the string the BMC will use as the community string
for PET alerts/traps.
:param community: The community string
:param channel: The LAN channel (defaults to auto detect)
"""
if channel is None:
channel = self.get_network_channel()
community = community.encode('utf-8')
community += b'\x00' * (18 - len(community))
cmddata = bytearray((channel, 16))
cmddata += community
self.xraw_command(netfn=0xc, command=1, data=cmddata)
def _assure_alert_policy(self, channel, destination):
"""Make sure an alert policy exists
Each policy will be a dict with the following keys:
-'index' - The policy index number
:returns: An iterable of currently configured alert policies
"""
# First we do a get PEF configuration parameters to get the count
# of entries. We have no guarantee that the meaningful data will
# be contiguous
rsp = self.xraw_command(netfn=4, command=0x13, data=(8, 0, 0))
numpol = ord(rsp['data'][1])
desiredchandest = (channel << 4) | destination
availpolnum = None
for polnum in range(1, numpol + 1):
currpol = self.xraw_command(netfn=4, command=0x13,
data=(9, polnum, 0))
polidx, chandest = struct.unpack_from('>BB', currpol['data'][2:4])
if not polidx & 0b1000:
if availpolnum is None:
availpolnum = polnum
continue
if chandest == desiredchandest:
return True
# If chandest did not equal desiredchandest ever, we need to use a slot
if availpolnum is None:
raise Exception("No available alert policy entry")
# 24 = 1 << 4 | 8
# 1 == set to which this rule belongs
# 8 == 0b1000, in other words, enable this policy, always send to
# indicated destination
self.xraw_command(netfn=4, command=0x12,
data=(9, availpolnum, 24,
desiredchandest, 0))
def get_alert_community(self, channel=None):
"""Get the current community string for alerts
Returns the community string that will be in SNMP traps from this
BMC
:param channel: The channel to get configuration for, autodetect by
default
:returns: The community string
"""
if channel is None:
channel = self.get_network_channel()
rsp = self.xraw_command(netfn=0xc, command=2, data=(channel, 16, 0, 0))
return rsp['data'][1:].partition('\x00')[0]
@property
def _supports_standard_ipv6(self):
# Supports the *standard* ipv6 commands for various things
# used to internally steer some commands to standard or OEM
# handler of commands
lanchan = self.get_network_channel()
if self._ipv6support is None:
rsp = self.raw_command(netfn=0xc, command=0x2, data=(2, lanchan,
0x32, 0, 0))
self._ipv6support = rsp['code'] == 0
return self._ipv6support
def set_alert_destination(self, ip=None, acknowledge_required=None,
acknowledge_timeout=None, retries=None,
destination=0, channel=None):
"""Configure one or more parameters of an alert destination
If any parameter is 'None' (default), that parameter is left unchanged.
Otherwise, all given parameters are set by this command.
:param ip: IP address of the destination. It is currently expected
that the calling code will handle any name lookup and
present this data as IP address.
:param acknowledge_required: Whether or not the target should expect
an acknowledgement from this alert target.
:param acknowledge_timeout: Time to wait for acknowledgement if enabled
:param retries: How many times to attempt transmit of an alert.
:param destination: Destination index, defaults to 0.
:param channel: The channel to configure the alert on. Defaults to
current
"""
if channel is None:
channel = self.get_network_channel()
if ip is not None:
destdata = bytearray((channel, 19, destination))
try:
parsedip = socket.inet_aton(ip)
destdata.extend((0, 0))
destdata.extend(parsedip)
destdata.extend(b'\x00\x00\x00\x00\x00\x00')
except socket.error:
if self._supports_standard_ipv6:
parsedip = socket.inet_pton(socket.AF_INET6, ip)
destdata.append(0b10000000)
destdata.extend(parsedip)
else:
destdata = None
self.oem_init()
self._oem.set_alert_ipv6_destination(ip, destination,
channel)
if destdata:
self.xraw_command(netfn=0xc, command=1, data=destdata)
if (acknowledge_required is not None or retries is not None or
acknowledge_timeout is not None):
currtype = self.xraw_command(netfn=0xc, command=2, data=(
channel, 18, destination, 0))
if currtype['data'][0] != b'\x11':
raise exc.PyghmiException("Unknown parameter format")
currtype = bytearray(currtype['data'][1:])
if acknowledge_required is not None:
if acknowledge_required:
currtype[1] |= 0b10000000
else:
currtype[1] &= 0b1111111
if acknowledge_timeout is not None:
currtype[2] = acknowledge_timeout
if retries is not None:
currtype[3] = retries
destreq = bytearray((channel, 18))
destreq.extend(currtype)
self.xraw_command(netfn=0xc, command=1, data=destreq)
if not ip == '0.0.0.0':
self._assure_alert_policy(channel, destination)
def get_mci(self):
"""Get the Management Controller Identifier, per DCMI specification
:returns: The identifier as a string
"""
return self._chunkwise_dcmi_fetch(9)
def set_mci(self, mci):
"""Set the management controller identifier, per DCMI specification
"""
return self._chunkwise_dcmi_set(0xa, mci + b'\x00')
def get_asset_tag(self):
"""Get the system asset tag, per DCMI specification
:returns: The asset tag
"""
return self._chunkwise_dcmi_fetch(6)
def set_asset_tag(self, tag):
"""Set the asset tag value
"""
return self._chunkwise_dcmi_set(8, tag)
def _chunkwise_dcmi_fetch(self, command):
szdata = self.xraw_command(
netfn=0x2c, command=command, data=(0xdc, 0, 0))
totalsize = ord(szdata['data'][1])
chksize = 0xf
offset = 0
retstr = ''
while offset < totalsize:
if (offset + chksize) > totalsize:
chksize = totalsize - offset
chk = self.xraw_command(
netfn=0x2c, command=command, data=(0xdc, offset, chksize))
retstr += chk['data'][2:]
offset += chksize
return retstr
def _chunkwise_dcmi_set(self, command, data):
chunks = [data[i:i + 15] for i in range(0, len(data), 15)]
offset = 0
for chunk in chunks:
chunk = bytearray(chunk, 'utf-8')
cmddata = bytearray((0xdc, offset, len(chunk)))
cmddata += chunk
self.xraw_command(netfn=0x2c, command=command, data=cmddata)
def set_channel_access(self, channel=None,
access_update_mode='non_volatile',
alerting=False, per_msg_auth=False,
user_level_auth=False, access_mode='always',
privilege_update_mode='non_volatile',
privilege_level='administrator'):
"""Set channel access
:param channel: number [1:7]
:param access_update_mode:
dont_change = don't set or change Channel Access
non_volatile = set non-volatile Channel Access
volatile = set volatile (active) setting of Channel Access
:param alerting: PEF Alerting Enable/Disable
True = enable PEF Alerting
False = disable PEF Alerting on this channel
(Alert Immediate command can still be used to generate alerts)
:param per_msg_auth: Per-message Authentication
True = enable
False = disable Per-message Authentication. [Authentication required to
activate any session on this channel, but authentication not
used on subsequent packets for the session.]
:param user_level_auth: User Level Authentication Enable/Disable.
True = enable User Level Authentication. All User Level commands are
to be authenticated per the Authentication Type that was
negotiated when the session was activated.
False = disable User Level Authentication. Allow User Level commands to
be executed without being authenticated.
If the option to disable User Level Command authentication is
accepted, the BMC will accept packets with Authentication Type
set to None if they contain user level commands.
For outgoing packets, the BMC returns responses with the same
Authentication Type that was used for the request.
:param access_mode: Access Mode for IPMI messaging
(PEF Alerting is enabled/disabled separately from IPMI messaging)
disabled = disabled for IPMI messaging
pre_boot = pre-boot only channel only available when system is in a
powered down state or in BIOS prior to start of boot.
always = channel always available regardless of system mode.
BIOS typically dedicates the serial connection to the BMC.
shared = same as always available, but BIOS typically leaves the
serial port available for software use.
:param privilege_update_mode: Channel Privilege Level Limit.
This value sets the maximum privilege level
that can be accepted on the specified channel.
dont_change = don't set or change channel Privilege Level Limit
non_volatile = non-volatile Privilege Level Limit according
volatile = volatile setting of Privilege Level Limit
:param privilege_level: Channel Privilege Level Limit
* reserved = unused
* callback
* user
* operator
* administrator
* proprietary = used by OEM
"""
if channel is None:
channel = self.get_network_channel()
data = []
data.append(channel & 0b00001111)
access_update_modes = {
'dont_change': 0,
'non_volatile': 1,
'volatile': 2,
# 'reserved': 3
}
b = 0
b |= (access_update_modes[access_update_mode] << 6) & 0b11000000
if alerting:
b |= 0b00100000
if per_msg_auth:
b |= 0b00010000
if user_level_auth:
b |= 0b00001000
access_modes = {
'disabled': 0,
'pre_boot': 1,
'always': 2,
'shared': 3,
}
b |= access_modes[access_mode] & 0b00000111
data.append(b)
b = 0
privilege_update_modes = {
'dont_change': 0,
'non_volatile': 1,
'volatile': 2,
# 'reserved': 3
}
b |= (privilege_update_modes[privilege_update_mode] << 6) & 0b11000000
privilege_levels = {
'reserved': 0,
'callback': 1,
'user': 2,
'operator': 3,
'administrator': 4,
'proprietary': 5,
# 'no_access': 0x0F,
}
b |= privilege_levels[privilege_level] & 0b00000111
data.append(b)
response = self.raw_command(netfn=0x06, command=0x40, data=data)
if 'error' in response:
raise Exception(response['error'])
return True
def get_channel_access(self, channel=None, read_mode='volatile'):
"""Get channel access
:param channel: number [1:7]
:param read_mode:
non_volatile = get non-volatile Channel Access
volatile = get present volatile (active) setting of Channel Access
:return: A Python dict with the following keys/values:
{
- alerting:
- per_msg_auth:
- user_level_auth:
- access_mode:{
0: 'disabled',
1: 'pre_boot',
2: 'always',
3: 'shared'
}
- privilege_level: {
1: 'callback',
2: 'user',
3: 'operator',
4: 'administrator',
5: 'proprietary',
}
}
"""
if channel is None:
channel = self.get_network_channel()
data = []
data.append(channel & 0b00001111)
b = 0
read_modes = {
'non_volatile': 1,
'volatile': 2,
}
b |= (read_modes[read_mode] << 6) & 0b11000000
data.append(b)
response = self.raw_command(netfn=0x06, command=0x41, data=data)
if 'error' in response:
raise Exception(response['error'])
data = response['data']
if len(data) != 2:
raise Exception('expecting 2 data bytes')
r = {}
r['alerting'] = data[0] & 0b10000000 > 0
r['per_msg_auth'] = data[0] & 0b01000000 > 0
r['user_level_auth'] = data[0] & 0b00100000 > 0
access_modes = {
0: 'disabled',
1: 'pre_boot',
2: 'always',
3: 'shared'
}
r['access_mode'] = access_modes[data[0] & 0b00000011]
privilege_levels = {
0: 'reserved',
1: 'callback',
2: 'user',
3: 'operator',
4: 'administrator',
5: 'proprietary',
# 0x0F: 'no_access'
}
r['privilege_level'] = privilege_levels[data[1] & 0b00001111]
return r
def get_channel_info(self, channel=None):
"""Get channel info
:param channel: number [1:7]
:return:
session_support:
no_session: channel is session-less
single: channel is single-session
multi: channel is multi-session
auto: channel is session-based (channel could alternate between
single- and multi-session operation, as can occur with a
serial/modem channel that supports connection mode auto-detect)
"""
if channel is None:
channel = self.get_network_channel()
data = []
data.append(channel & 0b00001111)
response = self.raw_command(netfn=0x06, command=0x42, data=data)
if 'error' in response:
raise Exception(response['error'])
data = response['data']
if len(data) != 9:
raise Exception('expecting 10 data bytes got: {0}'.format(data))
r = {}
r['Actual channel'] = data[0] & 0b00000111
channel_medium_types = {
0: 'reserved',
1: 'IPMB',
2: 'ICMB v1.0',
3: 'ICMB v0.9',
4: '802.3 LAN',
5: 'Asynch. Serial/Modem (RS-232)',
6: 'Other LAN',
7: 'PCI SMBus',
8: 'SMBus v1.0/1.1',
9: 'SMBus v2.0',
0x0a: 'reserved for USB 1.x',
0x0b: 'reserved for USB 2.x',
0x0c: 'System Interface (KCS, SMIC, or BT)',
# 60h-7Fh: OEM
# all other reserved
}
t = data[1] & 0b01111111
if t in channel_medium_types:
r['Channel Medium type'] = channel_medium_types[t]
else:
r['Channel Medium type'] = 'OEM {:02X}'.format(t)
r['5-bit Channel IPMI Messaging Protocol Type'] = data[2] & 0b00001111
session_supports = {
0: 'no_session',
1: 'single',
2: 'multi',
3: 'auto'
}
r['session_support'] = session_supports[(data[3] & 0b11000000) >> 6]
r['active_session_count'] = data[3] & 0b00111111
r['Vendor ID'] = [data[4], data[5], data[6]]
r['Auxiliary Channel Info'] = [data[7], data[8]]
return r
def set_user_access(self, uid, channel=None, callback=False,
link_auth=True, ipmi_msg=True, privilege_level='user'):
"""Set user access
:param uid: user number [1:16]
:param channel: number [1:7]
:parm callback: User Restricted to Callback
False = User Privilege Limit is determined by the User Privilege Limit
parameter, below, for both callback and non-callback connections.
True = User Privilege Limit is determined by the User Privilege Limit
parameter for callback connections, but is restricted to Callback
level for non-callback connections. Thus, a user can only initiate
a Callback when they 'call in' to the BMC, but once the callback
connection has been made, the user could potentially establish a
session as an Operator.
:param link_auth: User Link authentication
enable/disable (used to enable whether this
user's name and password information will be used for link
authentication, e.g. PPP CHAP) for the given channel. Link
authentication itself is a global setting for the channel and is
enabled/disabled via the serial/modem configuration parameters.
:param ipmi_msg: User IPMI Messaginge:
(used to enable/disable whether
this user's name and password information will be used for IPMI
Messaging. In this case, 'IPMI Messaging' refers to the ability to
execute generic IPMI commands that are not associated with a
particular payload type. For example, if IPMI Messaging is disabled for
a user, but that user is enabled for activatallow_authing the SOL
payload type, then IPMI commands associated with SOL and session
management, such as Get SOL Configuration Parameters and Close Session
are available, but generic IPMI commands such as Get SEL Time are
unavailable.)
:param privilege_level:
User Privilege Limit. (Determines the maximum privilege level that the
user is allowed to switch to on the specified channel.)
* callback
* user
* operator
* administrator
* proprietary
* no_access
"""
if channel is None:
channel = self.get_network_channel()
b = 0b10000000
if callback:
b |= 0b01000000
if link_auth:
b |= 0b00100000
if ipmi_msg:
b |= 0b00010000
b |= channel & 0b00001111
privilege_levels = {
'reserved': 0,
'callback': 1,
'user': 2,
'operator': 3,
'administrator': 4,
'proprietary': 5,
'no_access': 0x0F,
}
data = [b, uid & 0b00111111,
privilege_levels[privilege_level] & 0b00001111, 0]
response = self.raw_command(netfn=0x06, command=0x43, data=data)
if 'error' in response:
raise Exception(response['error'])
return True
def get_user_access(self, uid, channel=None):
"""Get user access
:param uid: user number [1:16]
:param channel: number [1:7]
:return:
channel_info:
max_user_count = maximum number of user IDs on this channel
enabled_users = count of User ID slots presently in use
users_with_fixed_names = count of user IDs with fixed names
access:
callback
link_auth
ipmi_msg
privilege_level: [reserved, callback, user,
operatorm administrator, proprietary, no_access]
"""
# user access available during call-in or callback direct connection
if channel is None:
channel = self.get_network_channel()
data = [channel, uid]
response = self.raw_command(netfn=0x06, command=0x44, data=data)
if 'error' in response:
raise Exception(response['error'])
data = response['data']
if len(data) != 4:
raise Exception('expecting 4 data bytes')
r = {'channel_info': {}, 'access': {}}
r['channel_info']['max_user_count'] = data[0]
r['channel_info']['enabled_users'] = data[1] & 0b00111111
r['channel_info']['users_with_fixed_names'] = data[2] & 0b00111111
r['access']['callback'] = (data[3] & 0b01000000) != 0
r['access']['link_auth'] = (data[3] & 0b00100000) != 0
r['access']['ipmi_msg'] = (data[3] & 0b00010000) != 0
privilege_levels = {
0: 'reserved',
1: 'callback',
2: 'user',
3: 'operator',
4: 'administrator',
5: 'proprietary',
0x0F: 'no_access'
}
r['access']['privilege_level'] = privilege_levels[data[3] & 0b00001111]
return r
def set_user_name(self, uid, name):
"""Set user name
:param uid: user number [1:16]
:param name: username (limit of 16bytes)
"""
data = [uid]
if len(name) > 16:
raise Exception('name must be less than or = 16 chars')
name = name.ljust(16, "\x00")
data.extend([ord(x) for x in name])
self.xraw_command(netfn=0x06, command=0x45, data=data)
return True
def get_user_name(self, uid, return_none_on_error=True):
"""Get user name
:param uid: user number [1:16]
:param return_none_on_error: return None on error
TODO: investigate return code on error
"""
response = self.raw_command(netfn=0x06, command=0x46, data=(uid,))
if 'error' in response:
if return_none_on_error:
return None
raise Exception(response['error'])
name = None
if 'data' in response:
data = response['data']
if len(data) == 16:
# convert int array to string
n = ''.join(chr(data[i]) for i in range(0, len(data)))
# remove padded \x00 chars
n = n.rstrip("\x00")
if len(n) > 0:
name = n
return name
def set_user_password(self, uid, mode='set_password', password=None):
"""Set user password and (modes)
:param uid: id number of user. see: get_names_uid()['name']
:param mode:
disable = disable user connections
enable = enable user connections
set_password = set or ensure password
test_password = test password is correct
:param password: max 16 char string
(optional when mode is [disable or enable])
:return:
True on success
when mode = test_password, return False on bad password
"""
mode_mask = {
'disable': 0,
'enable': 1,
'set_password': 2,
'test_password': 3
}
data = [uid, mode_mask[mode]]
if password:
password = str(password)
if 21 > len(password) > 16:
password = password.ljust(20, b'\x00')
data[0] |= 0b10000000
elif len(password) > 20:
raise Exception('password has limit of 20 chars')
else:
password = password.ljust(16, "\x00")
data.extend([ord(x) for x in password])
try:
self.xraw_command(netfn=0x06, command=0x47, data=data)
except exc.IpmiException as ie:
if mode == 'test_password':
return False
elif mode in ('enable', 'disable') and ie.ipmicode == 0xcc:
# Some BMCs see redundant calls to password disable/enable
# as invalid
return True
raise
return True
def get_channel_max_user_count(self, channel=None):
"""Get max users in channel (helper)
:param channel: number [1:7]
:return: int -- often 16
"""
if channel is None:
channel = self.get_network_channel()
access = self.get_user_access(channel=channel, uid=1)
return access['channel_info']['max_user_count']
def get_user(self, uid, channel=None):
"""Get user (helper)
:param uid: user number [1:16]
:param channel: number [1:7]
:return:
name: (str)
uid: (int)
channel: (int)
access:
callback (bool)
link_auth (bool)
ipmi_msg (bool)
privilege_level: (str)[callback, user, operatorm administrator,
proprietary, no_access]
"""
if channel is None:
channel = self.get_network_channel()
name = self.get_user_name(uid)
access = self.get_user_access(uid, channel)
data = {'name': name, 'uid': uid, 'channel': channel,
'access': access['access']}
return data
def get_name_uids(self, name, channel=None):
"""get list of users (helper)
:param channel: number [1:7]
:return: list of users
"""
if channel is None:
channel = self.get_network_channel()
uid_list = []
max_ids = self.get_channel_max_user_count(channel)
for uid in range(1, max_ids):
if name == self.get_user_name(uid=uid):
uid_list.append(uid)
return uid_list
def get_users(self, channel=None):
"""get list of users and channel access information (helper)
:param channel: number [1:7]
:return:
name: (str)
uid: (int)
channel: (int)
access:
callback (bool)
link_auth (bool)
ipmi_msg (bool)
privilege_level: (str)[callback, user, operatorm administrator,
proprietary, no_access]
"""
if channel is None:
channel = self.get_network_channel()
names = {}
max_ids = self.get_channel_max_user_count(channel)
for uid in range(1, max_ids + 1):
name = self.get_user_name(uid=uid)
if name is not None:
names[uid] = self.get_user(uid=uid, channel=channel)
return names
def create_user(self, uid, name, password, channel=None, callback=False,
link_auth=True, ipmi_msg=True,
privilege_level='user'):
"""create/ensure a user is created with provided settings (helper)
:param privilege_level:
User Privilege Limit. (Determines the maximum privilege level that
the user is allowed to switch to on the specified channel.)
* callback
* user
* operator
* administrator
* proprietary
* no_access
"""
# current user might be trying to update.. dont disable
# set_user_password(uid, password, mode='disable')
if channel is None:
channel = self.get_network_channel()
self.set_user_name(uid, name)
self.set_user_access(uid, channel, callback=callback,
link_auth=link_auth, ipmi_msg=ipmi_msg,
privilege_level=privilege_level)
self.set_user_password(uid, password=password)
self.set_user_password(uid, mode='enable', password=password)
return True
def user_delete(self, uid, channel=None):
"""Delete user (helper)
Note that in IPMI, user 'deletion' isn't a concept. This function
will make a best effort to provide the expected result (e.g.
web interfaces skipping names and ipmitool skipping as well.
:param uid: user number [1:16]
:param channel: number [1:7]
"""
# TODO(jjohnson2): Provide OEM extensibility to cover user deletion
if channel is None:
channel = self.get_network_channel()
self.set_user_password(uid, mode='disable', password=None)
# TODO(steveweber) perhaps should set user access on all channels
# so new users dont get extra access
self.set_user_access(uid, channel=channel, callback=False,
link_auth=False, ipmi_msg=False,
privilege_level='no_access')
try:
# First try to set name to all \x00 explicitly
self.set_user_name(uid, '')
except exc.IpmiException as ie:
if ie.ipmicode != 0xcc:
raise
# An invalid data field in request is frequently reported.
# however another convention that exists is all '\xff'
# if this fails, pass up the error so that calling code knows
# that the deletion did not go as planned for now
self.set_user_name(uid, b'\xff' * 16)
return True
def disable_user(self, uid, mode):
"""Disable User
Just disable the User.
This will not disable the password or revoke privileges.
:param uid: user number [1:16]
:param mode:
disable = disable user connections
enable = enable user connections
"""
self.set_user_password(uid, mode)
return True
def get_firmware(self):
"""Retrieve OEM Firmware information
"""
self.oem_init()
mcinfo = self.xraw_command(netfn=6, command=1)
bmcver = '{0}.{1}'.format(
ord(mcinfo['data'][2]), hex(ord(mcinfo['data'][3]))[2:])
return self._oem.get_oem_firmware(bmcver)
def get_capping_enabled(self):
"""Get PSU based power capping status
:return: True if enabled and False if disabled
"""
self.oem_init()
return self._oem.get_oem_capping_enabled()
def set_capping_enabled(self, enable):
"""Set PSU based power capping
:param enable: True for enable and False for disable
"""
self.oem_init()
return self._oem.set_oem_capping_enabled(enable)
def get_remote_kvm_available(self):
"""Get remote KVM availability
"""
self.oem_init()
return self._oem.get_oem_remote_kvm_available()
def get_domain_name(self):
"""Get Domain name
"""
self.oem_init()
return self._oem.get_oem_domain_name()
def set_domain_name(self, name):
"""Set Domain name
:param name: domain name to be set
"""
self.oem_init()
self._oem.set_oem_domain_name(name)
def get_graphical_console(self):
"""Get graphical console launcher"""
self.oem_init()
return self._oem.get_graphical_console()
def update_firmware(self, file, data=None, progress=None, bank=None):
"""Send file to BMC to perform firmware update
:param filename: The filename to upload to the target BMC
:param data: The payload of the firmware. Default is to read from
specified filename.
:param progress: A callback that will be given a dict describing
update process. Provide if
:param bank: Indicate a target 'bank' of firmware if supported
"""
self.oem_init()
if progress is None:
progress = lambda x: True
return self._oem.update_firmware(file, data, progress, bank)
def attach_remote_media(self, url, username=None, password=None):
"""Attach remote media by url
Given a url, attach remote media (cd/usb image) to the target system.
:param url: URL to indicate where to find image (protocol support
varies by BMC)
:param username: Username for endpoint to use when accessing the URL.
If applicable, 'domain' would be indicated by '@' or
'\' syntax.
:param password: Password for endpoint to use when accessing the URL.
"""
self.oem_init()
return self._oem.attach_remote_media(url, username, password)
def detach_remote_media(self):
self.oem_init()
return self._oem.detach_remote_media()
def upload_media(self, filename, progress=None):
"""Upload a file to be hosted on the target BMC
This will upload the specified data to
the BMC so that it will make it available to the system as an emulated
USB device.
:param filename: The filename to use, the basename of the parameter
will be given to the bmc.
:param filename: Optional callback for progress updates
"""
self.oem_init()
return self._oem.upload_media(filename, progress)
def list_media(self):
"""List attached remote media
:returns: An iterable list of attached media
"""
self.oem_init()
return self._oem.list_media()
|