-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement system info API endpoint (#136)
- Loading branch information
1 parent
eb3c3f0
commit 2f3bdeb
Showing
7 changed files
with
151 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) 2022-present Mattermost, Inc. All Rights Reserved. | ||
// See LICENSE.txt for license information. | ||
|
||
package service | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/mattermost/mattermost/server/public/shared/mlog" | ||
|
||
"github.com/prometheus/procfs" | ||
) | ||
|
||
type SystemInfo struct { | ||
Load procfs.LoadAvg `json:"load"` | ||
} | ||
|
||
func (s *Service) getSystemInfo(w http.ResponseWriter, req *http.Request) { | ||
if req.Method != http.MethodGet { | ||
http.NotFound(w, req) | ||
return | ||
} | ||
|
||
var info SystemInfo | ||
avg, err := s.proc.LoadAvg() | ||
if err == nil { | ||
info.Load = *avg | ||
} else { | ||
s.log.Error("failed to get load average", mlog.Err(err)) | ||
} | ||
|
||
w.Header().Add("Content-Type", "application/json") | ||
if err := json.NewEncoder(w).Encode(&info); err != nil { | ||
s.log.Error("failed to encode data", mlog.Err(err)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (c) 2022-present Mattermost, Inc. All Rights Reserved. | ||
// See LICENSE.txt for license information. | ||
|
||
package service | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGetSystem(t *testing.T) { | ||
th := SetupTestHelper(t, nil) | ||
defer th.Teardown() | ||
|
||
t.Run("invalid method", func(t *testing.T) { | ||
resp, err := http.Post(th.apiURL+"/system", "", nil) | ||
require.NoError(t, err) | ||
require.Equal(t, http.StatusNotFound, resp.StatusCode) | ||
}) | ||
|
||
t.Run("valid response", func(t *testing.T) { | ||
resp, err := http.Get(th.apiURL + "/system") | ||
require.NoError(t, err) | ||
require.Equal(t, http.StatusOK, resp.StatusCode) | ||
defer resp.Body.Close() | ||
var info SystemInfo | ||
err = json.NewDecoder(resp.Body).Decode(&info) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, info.Load) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters