Skip to content

Instantly share code, notes, and snippets.

@ugjka
Created January 18, 2021 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ugjka/65c4db512a11ac452878abd69ca0f374 to your computer and use it in GitHub Desktop.
Save ugjka/65c4db512a11ac452878abd69ca0f374 to your computer and use it in GitHub Desktop.
System status Kdialog greeter
package main
import (
"bytes"
"fmt"
"log"
"os/exec"
"strings"
"syscall"
"time"
)
func main() {
//This would typically go in .xprofile
//Build up a status message and present it with kdialog
welcome := bytes.NewBuffer(nil)
// Boot info
uname := syscall.Utsname{}
err := syscall.Uname(&uname)
if err != nil {
log.Fatal(err)
}
fmt.Fprintf(welcome, "Welcome to %s: %s %s (%s)\n",
str(uname.Nodename[:]),
str(uname.Sysname[:]),
str(uname.Release[:]),
str(uname.Machine[:]))
fmt.Fprintln(welcome, "\nLast boot:\n----------")
last := try("last", "reboot", "--limit", "1")
date := strings.Fields(string(last))[4:8]
fmt.Fprintf(welcome, "%s\n\n", strings.Join(date, " "))
analyze := try("systemd-analyze")
analyze = bytes.Replace(analyze, []byte("graphical"), []byte("Graphichal"), 1)
fmt.Fprintf(welcome, "%s\n", analyze)
// Boot errors
fmt.Fprintln(welcome, "Boot errors:\n-------")
critical := try("journalctl", "-r", "-b", "--no-pager", "-p", "err", "-q")
if len(critical) == 0 {
fmt.Fprintln(welcome, "no errors detected")
} else {
fmt.Fprintf(welcome, "%s\n", critical)
}
// Failed systemd units
fmt.Fprintln(welcome, "Failed units:\n-------------")
failed := try("systemctl", "--failed")
fmt.Fprintf(welcome, "%s\n", failed)
// Last logins
fmt.Fprintln(welcome, "Last logins:\n------------")
last = try("last")
var i int
for _, entry := range strings.Split(string(last), "\n") {
if i == 5 {
break
}
if strings.Contains(entry, "reboot") {
continue
}
fields := strings.Fields(entry)
entry = fields[0] + "\t" + strings.Join(fields[2:7], " ")
fmt.Fprintln(welcome, entry)
i++
}
fmt.Fprintln(welcome, "\nNice to see you!")
kdialog := exec.Command("kdialog", "--textbox", "-", "1150", "800")
kdialog.Stdin = welcome
kdialog.Run()
}
func str(arr []int8) string {
b := make([]byte, 0, len(arr))
for _, v := range arr {
if v == 0x00 {
break
}
b = append(b, byte(v))
}
return string(b)
}
func try(name string, args ...string) (out []byte) {
var err error
for {
out, err = exec.Command(name, args...).Output()
if err == nil {
break
}
time.Sleep(time.Second)
}
return out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment