Скачать книгу

       for scanner.Scan() {

       xtmp = scanner.Text()

       if strings.Contains(xtmp, "%SYSTIME%") {

       xtmp = strings.Replace(xtmp, "%SYSTIME%", currentTime.Format("2006-01-02 15:04:05 Monday"), -1)

       }

       if strings.Contains(xtmp, "%OSENVIRONMENT%") {

       xenv = ""

       for _, pair := range os.Environ() {

       variable := strings.Split(pair, "=")

       xenv += variable[0] + "=>" + variable[1] + "<br>"

       }

       xtmp = strings.Replace(xtmp, "%OSENVIRONMENT%", xenv, -1)

       }

       if strings.Contains(xtmp, "%DISKUSAGE%") {

       diskStat, err := disk.Usage("/")

       if err != nil {

       log.Println(" Plattenbelegung kann nicht gelesen werden: ", err)

       }

       xenv = "Pfad:" + diskStat.Path +

       "<br>FSTYPE:" + diskStat.Fstype +

       "<br>Total disk space:" + fmt.Sprintf("%5.1f", float64(diskStat.Total)/GB) +

       " GB<br>Free disk space:" + fmt.Sprintf("%5.1f", float64(diskStat.Free)/GB) +

       " GB<br>Used disk space:" + fmt.Sprintf("%5.1f", float64(diskStat.Used)/GB) +

       " GB<br>Used GB Prozent:" + fmt.Sprintf("%3.1f", diskStat.UsedPercent) +

       "<br>Used Inodes:" + strconv.FormatUint(diskStat.InodesUsed, 10) +

       "<br>Used Inodes Prozent:" + fmt.Sprintf("%3.1f", diskStat.InodesUsedPercent)

       xtmp = strings.Replace(xtmp, "%DISKUSAGE%", xenv, -1)

       }

       if strings.Contains(xtmp, "%HOSTINFO%") {

       // host or machine kernel, uptime, platform Info

       hostStat, err := host.Info()

       if err != nil {

       log.Println(" Hostinformation kann nicht gelesen werden: ", err)

       }

       xenv = "Hostname: " + hostStat.Hostname +

       "<br>OS: " + hostStat.OS +

       "<br>Platform: " + hostStat.Platform +

       "<br>Host ID(uuid): " + hostStat.HostID +

       "<br>Uptime (sec): " + strconv.FormatUint(hostStat.Uptime, 10) +

       "<br>Number of processes running: " + strconv.FormatUint(hostStat.Procs, 10)

       xtmp = strings.Replace(xtmp, "%HOSTINFO%", xenv, -1)

       }

       if strings.Contains(xtmp, "%MEMINFO%") {

       runtimeOS := runtime.GOOS

       vmStat, err := mem.VirtualMemory()

       if err != nil {

       log.Println(" Memoryinformation kann nicht gelesen werden: ", err)

       }

       xenv = "OS : " + runtimeOS +

       "<br>Total memory: " + fmt.Sprintf("%5.1f", float64(vmStat.Total)/GB) +

       " GB<br>Free memory: " + fmt.Sprintf("%5.1f", float64(vmStat.Free)/GB) +

       " GB<br>Used memory: " + fmt.Sprintf("%5.1f", float64(vmStat.Used)/GB) +

       " GB<br>Percentage used memory: " + strconv.FormatFloat(vmStat.UsedPercent, 'f', 2, 64)

       xtmp = strings.Replace(xtmp, "%MEMINFO%", xenv, -1)

       }

       xhtml += xtmp

       }

       if err := scanner.Err(); err != nil {

       log.Println(filename+" kann nicht gelesen werden: %s\n", err)

       xhtml = loadError(err.Error())

       }

       }

       defer f.Close()

       return &Page{Title: title, Body: []byte(xhtml)}, nil

      }

      func viewHandler(w http.ResponseWriter, r *http.Request) {

       title := r.URL.Path[len("/view/"):]

       p, _ := loadPage(title)

       fmt.Fprintf(w, "%s", p.Body)

      }

      func main() {

       log.Println("Main Started")

       http.HandleFunc("/view/", viewHandler)

       log.Fatal(http.ListenAndServe(":8080", nil))

       log.Println("Main End")

      }

      Um die Abhängigkeiten richtig auflösen zu können, braucht go auch ein module-File. Diese Dateien werden vom SDK LiteIDE selbstständig erzeugt.

      alfred@monitoring:~/GetInfo$ cat go.mod

      module GetInfo

      go 1.16

      require (

       github.com/StackExchange/wmi v1.2.1 // indirect

       github.com/shirou/gopsutil v3.21.9+incompatible // indirect

       github.com/tklauser/go-sysconf v0.3.9 // indirect

       golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac // indirect

      )

      alfred@monitoring:~/GetInfo$ cat go.sum

      github.com/StackExchange/wmi v1.2.1 h1:VIkavFPXSjcnS+O8yTq7NI32k0R5Aj+v39y29VYDOSA=

      github.com/StackExchange/wmi v1.2.1/go.mod h1:rcmrprowKIVzvc+NUiLncP2uuArMWLCbu9SBzvHz7e8=

      github.com/go-ole/go-ole v1.2.5 h1:t4MGB5xEDZvXI+0rMjjsfBsD7yAgp/s9ZDkL1JndXwY=

      github.com/go-ole/go-ole v1.2.5/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=

      github.com/shirou/gopsutil v3.21.9+incompatible h1:LTLpUnfX81MkHeCtSrwNKZwuW5Id6kCa7/P43NdcNn4=

      github.com/shirou/gopsutil v3.21.9+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=

      github.com/tklauser/go-sysconf v0.3.9 h1:JeUVdAOWhhxVcU6Eqr/ATFHgXk/mmiItdKeJPev3vTo=

      github.com/tklauser/go-sysconf v0.3.9/go.mod h1:11DU/5sG7UexIrp/O6g35hrWzu0JxlwQ3LSFUzyeuhs=

      github.com/tklauser/numcpus v0.3.0 h1:ILuRUQBtssgnxw0XXIjKUC56fgnOrFoQQ/4+DeU2biQ=

      github.com/tklauser/numcpus v0.3.0/go.mod h1:yFGUr7TUHQRAhyqBcEg0Ge34zDBAsIvJJcyE6boqnA8=

      golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

      golang.org/x/sys v0.0.0-20210816074244-15123e1e1f71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

      golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac h1:oN6lz7iLW/YC7un8pq+9bOLyXrprv2+DKfkJY+2LJJw=

      golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

      alfred@monitoring:~/GetInfo$

      Dieses Programm zeigt nach dem Aufruf von <Link:8080/view/filename> das aufgerufene

Скачать книгу