istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/ctrlz/topics/proc.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package topics
    16  
    17  import (
    18  	"net/http"
    19  	"os"
    20  	"runtime"
    21  
    22  	"istio.io/istio/pkg/ctrlz/fw"
    23  	"istio.io/istio/pkg/ctrlz/topics/assets"
    24  )
    25  
    26  type procTopic struct{}
    27  
    28  // ProcTopic returns a ControlZ topic that allows visualization of process state.
    29  func ProcTopic() fw.Topic {
    30  	return procTopic{}
    31  }
    32  
    33  func (procTopic) Title() string {
    34  	return "Process Info"
    35  }
    36  
    37  func (procTopic) Prefix() string {
    38  	return "proc"
    39  }
    40  
    41  type procInfo struct {
    42  	Egid       int    `json:"egid"`
    43  	Euid       int    `json:"euid"`
    44  	GID        int    `json:"gid"`
    45  	Groups     []int  `json:"groups"`
    46  	Pid        int    `json:"pid"`
    47  	Ppid       int    `json:"ppid"`
    48  	UID        int    `json:"uid"`
    49  	Wd         string `json:"wd"`
    50  	Hostname   string `json:"hostname"`
    51  	TempDir    string `json:"tempdir"`
    52  	Threads    int    `json:"threads"`
    53  	Goroutines int    `json:"goroutines"`
    54  }
    55  
    56  func getProcInfo() *procInfo {
    57  	pi := procInfo{
    58  		Egid:       os.Getegid(),
    59  		Euid:       os.Geteuid(),
    60  		GID:        os.Getgid(),
    61  		Pid:        os.Getpid(),
    62  		Ppid:       os.Getppid(),
    63  		UID:        os.Getuid(),
    64  		TempDir:    os.TempDir(),
    65  		Goroutines: runtime.NumGoroutine(),
    66  	}
    67  
    68  	pi.Groups, _ = os.Getgroups()
    69  	pi.Wd, _ = os.Hostname()
    70  	pi.Hostname, _ = os.Hostname()
    71  	pi.Threads, _ = runtime.ThreadCreateProfile(nil)
    72  
    73  	return &pi
    74  }
    75  
    76  func (procTopic) Activate(context fw.TopicContext) {
    77  	tmpl := assets.ParseTemplate(context.Layout(), "templates/proc.html")
    78  
    79  	_ = context.HTMLRouter().StrictSlash(true).NewRoute().Path("/").HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
    80  		fw.RenderHTML(w, tmpl, getProcInfo())
    81  	})
    82  
    83  	_ = context.JSONRouter().StrictSlash(true).NewRoute().Methods("GET").Path("/").HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
    84  		fw.RenderJSON(w, http.StatusOK, getProcInfo())
    85  	})
    86  }