Skip to content

Commit 7ce9047

Browse files
committed
port GenericShowtech to fboss2
1 parent e76fc15 commit 7ce9047

File tree

7 files changed

+311
-5
lines changed

7 files changed

+311
-5
lines changed

cmake/CliFboss2.cmake

+1
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ add_executable(fboss2
422422
fboss/cli/fboss2/commands/show/systemport/CmdShowSystemPort.h
423423
fboss/cli/fboss2/commands/show/cpuport/CmdShowCpuPort.h
424424
fboss/cli/fboss2/commands/show/platformshowtech/CmdShowPlatformShowtech.h
425+
fboss/cli/fboss2/commands/show/platformshowtech/Showtech.cpp
425426
fboss/cli/fboss2/commands/show/platformshowtech/utils.cpp
426427
fboss/cli/fboss2/commands/show/teflow/CmdShowTeFlow.h
427428
fboss/cli/fboss2/commands/show/transceiver/CmdShowTransceiver.h

fboss/cli/fboss2/BUCK

+1
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,7 @@ cpp_library(
458458
"commands/show/sdk/dump/CmdShowSdkDump.h",
459459
"commands/show/systemport/CmdShowSystemPort.h",
460460
"commands/show/platformshowtech/CmdShowPlatformShowtech.h",
461+
"commands/show/platformshowtech/Showtech.h",
461462
"commands/show/platformshowtech/utils.h",
462463
"commands/show/teflow/CmdShowTeFlow.h",
463464
"commands/show/transceiver/CmdShowTransceiver.h",

fboss/cli/fboss2/commands/show/platformshowtech/CmdShowPlatformShowtech.h

+16-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "fboss/cli/fboss2/CmdLocalOptions.h"
77
#include "fboss/cli/fboss2/commands/show/platformshowtech/gen-cpp2/model_types.h"
88
#include "fboss/cli/fboss2/utils/CmdClientUtils.h"
9+
#include "fboss/cli/fboss2/commands/show/platformshowtech/Showtech.h"
910

1011
namespace facebook::fboss {
1112

@@ -23,6 +24,8 @@ class CmdShowPlatformShowtech : public CmdHandler<CmdShowPlatformShowtech,
2324
public:
2425
using ObjectArgType = CmdShowPlatformShowtechTraits::ObjectArgType;
2526
using RetType = CmdShowPlatformShowtechTraits::RetType;
27+
using Showtech = show::platformshowtech::Showtech;
28+
using GenericShowtech = show::platformshowtech::GenericShowtech;
2629

2730
std::string version = "1.4";
2831

@@ -47,7 +50,19 @@ class CmdShowPlatformShowtech : public CmdHandler<CmdShowPlatformShowtech,
4750
}
4851

4952
void printOutput(const RetType& model, std::ostream& out = std::cout) {
50-
out << "unimplemented" << std::endl;
53+
bool verbose = true;
54+
std::unique_ptr<Showtech> platformShowtech;
55+
56+
if (model.get_quite()) {
57+
verbose = false;
58+
}
59+
60+
platformShowtech = get_platform_showtech(verbose);
61+
platformShowtech->printShowtech();
62+
}
63+
64+
std::unique_ptr<Showtech> get_platform_showtech(bool verbose) {
65+
return std::make_unique<GenericShowtech>(verbose);
5166
}
5267
};
5368

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
2+
3+
#include "fboss/cli/fboss2/commands/show/platformshowtech/Showtech.h"
4+
#include "fboss/cli/fboss2/commands/show/platformshowtech/utils.h"
5+
#include <filesystem>
6+
#include <iostream>
7+
#include <sstream>
8+
#include <string>
9+
#include <vector>
10+
11+
namespace facebook::fboss::show::platformshowtech {
12+
13+
namespace showtechUtils = show::platformshowtech::utils;
14+
15+
void Showtech::printVersion() {
16+
std::cout << "################################\n";
17+
std::cout << "##### SHOWTECH VERSION " << version << " #####\n";
18+
std::cout << "################################\n\n";
19+
}
20+
21+
void Showtech::printCpuDetails() {
22+
std::cout << "##### CPU SYSTEM TIME #####\n"
23+
<< showtechUtils::run_cmd_no_check("date");
24+
std::cout << "\n##### CPU HOSTNAME #####\n"
25+
<< showtechUtils::run_cmd_no_check("hostname");
26+
std::cout << "\n##### CPU Linux Kernel Version #####\n"
27+
<< showtechUtils::run_cmd_no_check("uname -r");
28+
std::cout << "\n##### CPU UPTIME #####\n"
29+
<< showtechUtils::run_cmd_no_check("uptime") << std::endl;
30+
}
31+
32+
void Showtech::printFbossDetails() {
33+
showtechUtils::print_fboss2_show_cmd("product");
34+
showtechUtils::print_fboss2_show_cmd("version agent");
35+
showtechUtils::print_fboss2_show_cmd("environment sensor");
36+
showtechUtils::print_fboss2_show_cmd("environment temperature");
37+
showtechUtils::print_fboss2_show_cmd("environment fan");
38+
showtechUtils::print_fboss2_show_cmd("environment power");
39+
}
40+
41+
void Showtech::printWeutil(std::string target) {
42+
std::string cmd = "weutil --eeprom " + target;
43+
std::filesystem::path ossConfigPath{
44+
"/opt/fboss/share/platform_configs/weutil.json"};
45+
46+
if (std::filesystem::exists(ossConfigPath)) {
47+
// OSS doesn't support running weutil without the -config_file arg.
48+
cmd = cmd + " -config_file " + ossConfigPath.string();
49+
}
50+
std::cout << "##### " + target + " SERIAL NUMBER #####\n";
51+
std::cout << showtechUtils::run_cmd_no_check(cmd) << std::endl;
52+
}
53+
54+
void Showtech::printLspci() {
55+
std::string cmd;
56+
57+
std::cout << "################################\n";
58+
std::cout << "############# LSPCI ############\n";
59+
std::cout << "################################\n\n";
60+
61+
cmd = "lspci";
62+
if (verbose_) {
63+
cmd = cmd + " -vvv";
64+
}
65+
std::cout << cmd << std::endl
66+
<< showtechUtils::run_cmd_no_check(cmd) << std::endl;
67+
}
68+
69+
void Showtech::printI2cDetect() {
70+
std::string cmd;
71+
std::set<int> bus_to_ignore = i2cBusIgnore();
72+
int bus;
73+
74+
std::cout << "################################\n";
75+
std::cout << "########## I2C DETECT ##########\n";
76+
std::cout << "################################\n\n";
77+
78+
cmd = "i2cdetect -l";
79+
std::cout << cmd << std::endl
80+
<< showtechUtils::run_cmd_no_check(cmd) << std::endl;
81+
82+
for (bus = 0; bus <= showtechUtils::get_max_i2c_bus(); ++bus) {
83+
if (bus_to_ignore.find(bus) == bus_to_ignore.end()) {
84+
cmd = "i2cdetect -y " + std::to_string(bus);
85+
std::cout << cmd << std::endl
86+
<< showtechUtils::run_cmd_no_check(cmd) << std::endl;
87+
}
88+
}
89+
}
90+
91+
void Showtech::printLogs() {
92+
std::cout << "################################\n";
93+
std::cout << "########## DEBUG LOGS ##########\n";
94+
std::cout << "################################\n\n";
95+
96+
std::cout << "#### SENSORS LOG ####\n";
97+
std::cout << showtechUtils::run_cmd_with_limit("journalctl -u sensor_service")
98+
<< std::endl;
99+
100+
std::cout << "#### FAN LOG ####\n";
101+
std::cout << showtechUtils::run_cmd_with_limit("journalctl -u fan_service")
102+
<< std::endl;
103+
104+
std::cout << "#### DATA CORRAL LOG ####\n";
105+
std::cout << showtechUtils::run_cmd_with_limit("journalctl -u data_corral_service")
106+
<< std::endl;
107+
108+
std::cout << "#### QSFP LOG ####\n";
109+
std::cout << showtechUtils::run_cmd_with_limit("journalctl -u qsfp_service")
110+
<< std::endl;
111+
112+
std::cout << "#### SW AGENT LOG ####\n";
113+
std::cout << showtechUtils::run_cmd_with_limit("journalctl -u fboss_sw_agent")
114+
<< std::endl;
115+
116+
std::cout << "#### HW AGENT LOG ####\n";
117+
std::cout << showtechUtils::run_cmd_with_limit("journalctl -u fboss_hw_agent@0")
118+
<< std::endl;
119+
120+
std::cout << "#### DMESG LOG ####\n";
121+
std::cout << showtechUtils::run_cmd_with_limit("dmesg")
122+
<< std::endl;
123+
124+
std::cout << "#### BOOT CONSOLE LOG ####\n";
125+
std::cout << showtechUtils::run_cmd_with_limit("cat /var/log/boot.log")
126+
<< std::endl;
127+
128+
std::cout << "#### LINUX MESSAGES LOG ####\n";
129+
std::cout << showtechUtils::run_cmd_with_limit("cat /var/log/messages")
130+
<< std::endl;
131+
}
132+
133+
void Showtech::printL1Info() {
134+
std::cout << "################################\n";
135+
std::cout << "########### L1 LOGS ############\n";
136+
std::cout << "################################\n\n";
137+
138+
showtechUtils::print_fboss2_show_cmd("port");
139+
showtechUtils::print_fboss2_show_cmd("fabric");
140+
showtechUtils::print_fboss2_show_cmd("lldp");
141+
showtechUtils::print_fboss2_show_cmd("interface counters");
142+
showtechUtils::print_fboss2_show_cmd("interface errors");
143+
showtechUtils::print_fboss2_show_cmd("interface flaps");
144+
showtechUtils::print_fboss2_show_cmd("interface phy");
145+
showtechUtils::print_fboss2_show_cmd("transceiver");
146+
147+
if (verbose_) {
148+
std::cout << "#### wedge_qsfp_util ####\n";
149+
std::cout << showtechUtils::run_cmd_no_check("wedge_qsfp_util") << std::endl;
150+
}
151+
}
152+
153+
void Showtech::printShowtech() {
154+
printVersion();
155+
printCpuDetails();
156+
printFbossDetails();
157+
printPlatformInfo();
158+
printLspci();
159+
printL1Info();
160+
if (verbose_) {
161+
printI2cDetect();
162+
printLogs();
163+
}
164+
}
165+
166+
} // namespace facebook::fboss::show::platformshowtech
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
2+
3+
#pragma once
4+
5+
#include <set>
6+
#include <string>
7+
8+
namespace facebook::fboss::show::platformshowtech {
9+
10+
class Showtech {
11+
public:
12+
Showtech(bool verbose) { verbose_ = verbose; }
13+
virtual ~Showtech() = default;
14+
void printShowtech();
15+
16+
std::string version = "1.4";
17+
18+
protected:
19+
// These should be common between platforms.
20+
void printVersion();
21+
void printCpuDetails();
22+
void printFbossDetails();
23+
void printWeutil(std::string target);
24+
void printLspci();
25+
void printI2cDetect();
26+
void printL1Info();
27+
void printLogs();
28+
29+
// Each platform overrides platform-specific info here.
30+
virtual void printPlatformInfo() = 0;
31+
virtual std::set<int> i2cBusIgnore() = 0;
32+
33+
bool verbose_;
34+
};
35+
36+
class GenericShowtech : public Showtech {
37+
public:
38+
GenericShowtech(bool verbose) : Showtech(verbose) {}
39+
void printPlatformInfo() override{};
40+
std::set<int> i2cBusIgnore() override { return {}; }
41+
};
42+
} // facebook::fboss::show::platformshowtech

fboss/cli/fboss2/commands/show/platformshowtech/utils.cpp

+76-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,80 @@
22

33
#include "fboss/cli/fboss2/commands/show/platformshowtech/utils.h"
44

5-
namespace facebook::fboss::show::tech::utils {
5+
namespace facebook::fboss::show::platformshowtech::utils {
66

7-
} // namespace facebook::fboss::show::tech::utils
7+
int run_cmd(std::string cmd, std::string &output) {
8+
std::array<char, 128> buffer;
9+
std::string result;
10+
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"),
11+
pclose);
12+
13+
if (!pipe) {
14+
return -1;
15+
}
16+
17+
while (std::fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
18+
result += buffer.data();
19+
}
20+
output = result;
21+
22+
return 0;
23+
}
24+
25+
std::string run_cmd_no_check(std::string cmd) {
26+
std::string output;
27+
28+
run_cmd(cmd, output);
29+
return output;
30+
}
31+
32+
std::string run_cmd_with_limit(std::string cmd, int max_lines) {
33+
std::string count_cmd = cmd + " | wc -l";
34+
int total_lines = std::stoi(run_cmd_no_check(count_cmd));
35+
36+
if (total_lines <= max_lines) {
37+
return run_cmd_no_check(cmd);
38+
}
39+
40+
int half_max = max_lines / 2;
41+
std::string first_part =
42+
run_cmd_no_check(cmd + " | head -n " + std::to_string(half_max));
43+
std::string last_part =
44+
run_cmd_no_check(cmd + " | tail -n " + std::to_string(half_max));
45+
46+
std::string truncation_message =
47+
"=== File exceeds " + std::to_string(max_lines) +
48+
" lines (total: " + std::to_string(total_lines) +
49+
"). Showing first and last " + std::to_string(half_max) +
50+
" lines only ===\n\n";
51+
52+
return truncation_message + first_part +
53+
"\n\n=== " + std::to_string(total_lines - max_lines) +
54+
" lines truncated ===\n\n" + last_part;
55+
}
56+
57+
void print_fboss2_show_cmd(std::string cmd) {
58+
if (!std::filesystem::exists("/etc/ramdisk")) {
59+
std::cout << "#### fboss2 show " << cmd << " ####\n";
60+
std::cout << run_cmd_no_check("fboss2 show " + cmd) << std::endl;
61+
}
62+
}
63+
64+
void strip(std::string &str) {
65+
str.erase(remove_if(str.begin(), str.end(), ::isspace), str.end());
66+
}
67+
68+
int get_max_i2c_bus() {
69+
std::string output;
70+
71+
if (run_cmd("/usr/sbin/i2cdetect -l | awk '{print $1}' | "
72+
"sed -e 's/i2c-//' | sort -n | tail -n 1",
73+
output)) {
74+
return 0;
75+
}
76+
77+
strip(output);
78+
return std::stoi(output);
79+
}
80+
81+
} // namespace facebook::fboss::show::platformshowtech::utils

fboss/cli/fboss2/commands/show/platformshowtech/utils.h

+9-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
#include "fboss/cli/fboss2/commands/show/platformshowtech/gen-cpp2/model_types.h"
77
#include "fboss/cli/fboss2/utils/CmdUtils.h"
88

9-
namespace facebook::fboss::show::tech::utils {
9+
namespace facebook::fboss::show::platformshowtech::utils {
1010

11-
} // namespace facebook::fboss::show::tech::utils
11+
int run_cmd(std::string cmd, std::string &output);
12+
std::string run_cmd_no_check(std::string cmd);
13+
std::string run_cmd_with_limit(std::string cmd, int max_lines = 5000);
14+
void print_fboss2_show_cmd(std::string cmd);
15+
void strip(std::string &str);
16+
int get_max_i2c_bus();
17+
18+
} // namespace facebook::fboss::show::platformshowtech::utils

0 commit comments

Comments
 (0)