Skip to content

Commit 5f30e38

Browse files
authored
Merge branch 'cloud-barista:master' into master
2 parents 2c1a362 + ba2cc02 commit 5f30e38

129 files changed

Lines changed: 6861 additions & 2200 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

api-runtime/common-runtime/SystemStatsHandler.go

Lines changed: 636 additions & 0 deletions
Large diffs are not rendered by default.

api-runtime/rest-runtime/CBSpiderRuntime.go

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@ import (
1414
"crypto/x509"
1515
"fmt"
1616
"io/ioutil"
17+
"log"
1718
"path/filepath"
1819
"strings"
1920
"time"
2021

2122
"net/http"
2223
"os"
2324

24-
"github.com/chyeh/pubip"
25-
2625
cblogger "github.com/cloud-barista/cb-log"
2726
cr "github.com/cloud-barista/cb-spider/api-runtime/common-runtime"
2827
aw "github.com/cloud-barista/cb-spider/api-runtime/rest-runtime/admin-web"
@@ -33,7 +32,7 @@ import (
3332
// REST API (echo)
3433
"github.com/labstack/echo/v4"
3534
"github.com/labstack/echo/v4/middleware"
36-
"github.com/labstack/gommon/log"
35+
lblog "github.com/labstack/gommon/log"
3736

3837
// echo-swagger middleware
3938
_ "github.com/cloud-barista/cb-spider/api"
@@ -105,7 +104,7 @@ func getServerIPorName(env string) string {
105104
hostEnv := os.Getenv(env) // SERVER_ADDRESS or SERVICE_ADDRESS
106105

107106
if hostEnv == "" {
108-
return getPublicIP()
107+
return "localhost"
109108
}
110109

111110
// "1.2.3.4" or "localhost"
@@ -115,26 +114,12 @@ func getServerIPorName(env string) string {
115114

116115
strs := strings.Split(hostEnv, ":")
117116
if strs[0] == "" { // ":31024"
118-
return getPublicIP()
117+
return "localhost"
119118
} else { // "1.2.3.4:31024" or "localhost:31024"
120119
return strs[0]
121120
}
122121
}
123122

124-
func getPublicIP() string {
125-
ip, err := pubip.Get()
126-
if err != nil {
127-
cblog.Error(err)
128-
hostName, err := os.Hostname()
129-
if err != nil {
130-
cblog.Error(err)
131-
}
132-
return hostName
133-
}
134-
135-
return ip.String()
136-
}
137-
138123
func getServerPort(env string) string {
139124
// default REST Service Port
140125
servicePort := ":1024"
@@ -201,6 +186,10 @@ func RunServer() {
201186
{"GET", "/ping", healthCheck},
202187
{"GET", "/readyz", healthCheck},
203188

189+
//----------SystemStatsInfo Handler
190+
{"GET", "/sysstats/system", FetchSystemInfo},
191+
{"GET", "/sysstats/usage", FetchResourceUsage},
192+
204193
//----------CloudOS
205194
{"GET", "/cloudos", ListCloudOS},
206195

@@ -527,6 +516,8 @@ func RunServer() {
527516

528517
{"GET", "/adminweb/spiderinfo", aw.SpiderInfo},
529518

519+
{"GET", "/adminweb/sysstats", aw.SystemStatsInfoPage},
520+
530521
{"GET", "/adminweb/vpc/:ConnectConfig", aw.VPCSubnetManagement},
531522
{"GET", "/adminweb/vpcmgmt/:ConnectConfig", aw.VPCMgmt},
532523
{"GET", "/adminweb/securitygroup/:ConnectConfig", aw.SecurityGroupManagement},
@@ -571,7 +562,7 @@ func RunServer() {
571562

572563
func RunTLSServer(certFile, keyFile, caCertFile string, port int) {
573564
e := echo.New()
574-
e.Logger.SetLevel(log.ERROR) // Set logging level to ERROR only
565+
e.Logger.SetLevel(lblog.ERROR) // Set logging level to ERROR only
575566

576567
// Recovery middleware for handling panics
577568
e.Use(middleware.Recover())
@@ -694,18 +685,24 @@ func ApiServer(routes []route) {
694685

695686
spiderBanner()
696687

697-
if err := e.Start(cr.ServerPort); err != nil {
698-
cblog.Fatalf("Failed to start the server: %v", err)
688+
httpServerPort := cr.ServerPort
689+
if cr.ServerIPorName == "localhost" || cr.ServerIPorName == "127.0.0.1" {
690+
// Bind to localhost only (127.0.0.1), external clients cannot connect
691+
httpServerPort = cr.ServerIPorName + cr.ServerPort
692+
}
693+
694+
server := &http.Server{
695+
Addr: httpServerPort,
696+
//ReadTimeout: 6000 * time.Second, // Increase the maximum duration of reading the entire request
697+
//WriteTimeout: 6000 * time.Second, // Increase the maximum duration of writing the entire response
698+
//IdleTimeout: 6000 * time.Second, // Increase the maximum duration of idle keep-alive connections
699+
MaxHeaderBytes: 500 * 1024 * 1024, // Increase the maximum header size allowed by the server
700+
ErrorLog: log.New(os.Stderr, "HTTP SERVER ERROR: ", log.LstdFlags),
699701
}
700702

701-
}
702-
703-
// ================ API Info
704-
func apiInfo(c echo.Context) error {
705-
cblog.Info("call apiInfo()")
706-
707-
apiInfo := "api info"
708-
return c.String(http.StatusOK, apiInfo)
703+
if err := e.StartServer(server); err != nil {
704+
cblog.Fatalf("Failed to start the server: %v", err)
705+
}
709706
}
710707

711708
// ================ Endpoint Info
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
// The CB-Spider is a sub-Framework of the Cloud-Barista Multi-Cloud Project.
2+
// The CB-Spider Mission is to connect all the clouds with a single interface.
3+
//
4+
// * Cloud-Barista: https://github.com/cloud-barista
5+
6+
package restruntime
7+
8+
import (
9+
"bytes"
10+
"io"
11+
"net/http"
12+
"os"
13+
14+
cmrt "github.com/cloud-barista/cb-spider/api-runtime/common-runtime"
15+
"github.com/labstack/echo/v4"
16+
)
17+
18+
// FetchSystemInfo godoc
19+
// @ID fetch-system-info
20+
// @Summary Fetch System Information
21+
// @Description Retrieve system information such as hostname, platform, CPU, memory, and disk.
22+
// @Description Use query parameter 'mode=text' to get the output in text format instead of JSON.
23+
// @Tags [Utility]
24+
// @Accept json
25+
// @Produce json,text/plain
26+
// @Param mode query string false "Output format: set to 'text' for text output (default is JSON)" Enums(text)
27+
// @Success 200 {object} cmrt.SystemInfo "System Information in JSON or text format based on 'mode' parameter"
28+
// @Failure 500 {object} SimpleMsg "Internal Server Error"
29+
// @Router /sysstats/system [get]
30+
func FetchSystemInfo(c echo.Context) error {
31+
cblog.Info("call FetchSystemInfo()")
32+
33+
// Get system information
34+
result, err := cmrt.FetchSystemInfo()
35+
if err != nil {
36+
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
37+
}
38+
39+
// Check output mode (text or json)
40+
mode := c.QueryParam("mode")
41+
42+
// If text mode, output as text format
43+
if mode == "text" {
44+
// Create buffer for console output
45+
var buffer bytes.Buffer
46+
47+
// Redirect output to buffer
48+
oldStdout := os.Stdout
49+
r, w, _ := os.Pipe()
50+
os.Stdout = w
51+
52+
// Output as text format
53+
cmrt.DisplaySystemInfo(&result)
54+
55+
// Close pipe
56+
w.Close()
57+
os.Stdout = oldStdout
58+
59+
// Read to buffer
60+
io.Copy(&buffer, r)
61+
62+
// Respond with text format
63+
c.Response().Header().Set(echo.HeaderContentType, "text/plain; charset=UTF-8")
64+
return c.String(http.StatusOK, buffer.String())
65+
}
66+
67+
// Default response in JSON format
68+
return c.JSON(http.StatusOK, result)
69+
}
70+
71+
// FetchResourceUsage godoc
72+
// @ID fetch-resource-usage
73+
// @Summary Fetch Resource Usage Information
74+
// @Description Retrieve resource usage information such as CPU, memory, disk I/O, and network I/O.
75+
// @Description Use query parameter 'mode=text' to get the output in text format instead of JSON.
76+
// @Tags [Utility]
77+
// @Accept json
78+
// @Produce json,text/plain
79+
// @Param mode query string false "Output format: set to 'text' for text output (default is JSON)" Enums(text)
80+
// @Success 200 {object} cmrt.ResourceUsage "Resource Usage Information in JSON or text format based on 'mode' parameter"
81+
// @Failure 500 {object} SimpleMsg "Internal Server Error"
82+
// @Router /sysstats/usage [get]
83+
func FetchResourceUsage(c echo.Context) error {
84+
cblog.Info("call FetchResourceUsage()")
85+
86+
// Get resource usage information
87+
resourceUsage, err := cmrt.FetchResourceUsage()
88+
if err != nil {
89+
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
90+
}
91+
92+
// Check output mode (text or json)
93+
mode := c.QueryParam("mode")
94+
95+
// If text mode, output as text format
96+
if mode == "text" {
97+
// Also get system info to get TotalMemory for display
98+
sysInfo, err := cmrt.FetchSystemInfo()
99+
if err != nil {
100+
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
101+
}
102+
103+
// Create buffer for console output
104+
var buffer bytes.Buffer
105+
106+
// Redirect output to buffer
107+
oldStdout := os.Stdout
108+
r, w, _ := os.Pipe()
109+
os.Stdout = w
110+
111+
// Output as text format
112+
cmrt.DisplayResourceUsage(sysInfo.TotalMemory, &resourceUsage)
113+
114+
// Close pipe
115+
w.Close()
116+
os.Stdout = oldStdout
117+
118+
// Read to buffer
119+
io.Copy(&buffer, r)
120+
121+
// Respond with text format
122+
c.Response().Header().Set(echo.HeaderContentType, "text/plain; charset=UTF-8")
123+
return c.String(http.StatusOK, buffer.String())
124+
}
125+
126+
// Default response in JSON format
127+
return c.JSON(http.StatusOK, resourceUsage)
128+
}

0 commit comments

Comments
 (0)