https://www.freecodecamp.org/news/how-to-remove-images-in-docker/
https://docs.docker.com/engine/reference/commandline/image_pull/
Dieses Kapitel beschreibt einen einfachen WebServer mit Go. Das ist ein richtig funktionierender WebService. Dieser Service wird alle Variablen und die Systemzeit des Containers wiedergeben.
Zuerst brauchen wir ein einfaches Programm.
// Ausgabe eines einfachen HTML-Files und ersetzen von Schlüsselwörtern mit konkreten Informationen
// Inspiration https://golang.org/doc/articles/wiki/
// https://www.socketloop.com/tutorials/golang-get-hardware-information-such-as-disk-memory-and-cpu-usage
//
// alfred@monitoring:~/GetInfo$ go get github.com/shirou/gopsutil/...
package main
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"runtime"
"strconv"
"strings"
"time"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/mem"
)
type Page struct {
Title string
Body []byte
}
func loadError(xerr string) (xhtml string) {
var xs string
currentTime := time.Now()
xtext, err := ioutil.ReadFile("error.html")
if err != nil {
log.Fatal("Error File kann nicht geöffnet werden " + err.Error())
}
xs = strings.Replace(string(xtext), "%SYSTIME%", currentTime.Format("2006-01-02 15:04:05 Monday"), -1)
xs = strings.Replace(string(xs), "%SYSTEMFEHLER%", xerr, -1)
return xs
}
func loadPage(title string) (*Page, error) {
var xhtml string
var xtmp string
var xenv string
const GB = 1073741824
filename := title + ".html"
log.Println(filename)
xhtml = ""
currentTime := time.Now()
f, err := os.Open(filename)
if err != nil {
log.Println(filename+" kann nicht geöffnet werden: ", err)
xhtml = loadError(err.Error())
} else {
scanner := bufio.NewScanner(f)
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] + "
"
}
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 +
"
FSTYPE:" + diskStat.Fstype +
"
Total disk space:" + fmt.Sprintf("%5.1f", float64(diskStat.Total)/GB) +
" GB
Free disk space:" + fmt.Sprintf("%5.1f", float64(diskStat.Free)/GB) +
" GB
Used disk space:" + fmt.Sprintf("%5.1f", float64(diskStat.Used)/GB) +
" GB
Used GB Prozent:" + fmt.Sprintf("%3.1f", diskStat.UsedPercent) +
"
Used Inodes:" + strconv.FormatUint(diskStat.InodesUsed, 10) +
"
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 +
"
OS: " + hostStat.OS +
"
Platform: " + hostStat.Platform +
"
Host ID(uuid): " + hostStat.HostID +
"
Uptime (sec): " + strconv.FormatUint(hostStat.Uptime, 10) +
"
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 +
"
Total memory: " + fmt.Sprintf("%5.1f", float64(vmStat.Total)/GB) +
" GB
Free memory: " + fmt.Sprintf("%5.1f", float64(vmStat.Free)/GB) +
" GB
Used memory: " + fmt.Sprintf("%5.1f", float64(vmStat.Used)/GB) +
" GB
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 das aufgerufene File an, und ersetzt gewisse Schlüsselwörter durch konkreten Text. In unserem Beispiel habe ich das File hello.html mit OpenOffice vorbereitet.
@page { size: 21cm 29.7cm; margin: 2cm }
p { margin-bottom: 0.25cm; line-height: 115%; background: transparent }
td p { orphans: 0; widows: 0; background: transparent }
OSENVIRONMENT
|
DISKUSAGE
|
HOSTINFO
|
MEMINFO
|
%OSENVIRONMENT%
|
%DISKUSAGE%
|
%HOSTINFO%
|
%MEMINFO%
|
Es ist %SYSTIME%
Nun kompilieren wir das File.
Читать дальше