docker와 virsh의 상태를 웹에 표시의 만든

12217 단어 5도커KVM
go로 WebUI를 만들고 싶었다.
coreos로 움직여두면 편리하다고 생각한다.

coreos timezone 설정
echo "export TZ='Asia/Tokyo'" >> ~/.profile

다운로드



tukiyo/host_status
  • http://localhost:9876/docker
  • http://localhost:9876/virsh

  • systemctl에 등록



    /etc/systemd/system/host_status.service
    [Unit]
    Description=host_status
    After=docker.service
    Requires=docker.service
    
    [Service]
    ExecStart=/home/core/host_status
    
    [Install]
    WantedBy=multi-user.target
    
    $ sudo systemctl enable host_status
    ln -s '/etc/systemd/system/host_status.service' '/etc/systemd/system/multi-user.target.wants/host_status.service'
    

    처리



    host_status.go
    package main
    
    import (
        "fmt"
        "net/http"
        "os/exec"
    )
    
    func main() {
        http.HandleFunc("/docker", docker)
        http.HandleFunc("/virsh", virsh)
        http.Handle("/", http.FileServer(http.Dir("/")))
        http.ListenAndServe(":9876", nil)
    }
    
    func docker(w http.ResponseWriter, r *http.Request) {
        header(w)
        print_hr(w, run("w"))
        print_hr(w, run("free -m"))
        print_hr(w, run("(df -hm | head -n 1) && (df -hm | sort -nr -k2 | head -n 5)"))
        print_hr(w, run("docker ps -a"))
        print_hr(w, run("for i in $(docker ps -a | grep -v '^CONTAINER' | awk '{print $1}'); do echo -n $i': '; docker inspect --format '{{ .NetworkSettings.IPAddress }}' $i;done"))
        print_hr(w, run("docker images"))
        print_hr(w, run("ps aux --sort user,-rss | grep -v '^root'"))
    }
    
    func virsh(w http.ResponseWriter, r *http.Request) {
        header(w)
        print_hr(w, run("virsh list --all"))
        print_hr(w, run("virsh net-list"))
        print_hr(w, run("virsh iface-list"))
        print_hr(w, run("virsh nodeinfo"))
        print_hr(w, run("virsh help"))
    }
    
    func run(strCommand string) string {
        cmd := exec.Command("/usr/bin/bash", "-c", strCommand)
        d, _ := cmd.Output()
        return string(d)
    }
    
    func print_hr(w http.ResponseWriter, stdout string) {
        fmt.Fprintf(w, "<hr>")
        fmt.Fprintf(w, string(stdout))
    }
    
    func header(w http.ResponseWriter) {
        w.Header().Add("Content-type", "text/html charset=utf-8")
        fmt.Fprintf(w, "<style>a{padding-right:1em;}</style>")
        fmt.Fprintf(w, "<a href='/docker'>[docker]</a>")
        fmt.Fprintf(w, "<a href='/virsh'>[virsh]</a>")
        fmt.Fprintf(w, "<a href='/var/lib/libvirt/images/'>virsh/images</a>")
        fmt.Fprintf(w, "<a href='/etc/'>/etc/</a>")
        fmt.Fprintf(w, "<a href='/var/log/'>/var/log/</a>")
        fmt.Fprintf(w, "<a href='/var/lib'>/var/lib/</a>")
        fmt.Fprintf(w, "<pre style='font-size:small;'>")
    }
    

    시작 방법



    루트로 실행
    go run host_status.go
    

    도커



    virsh

    좋은 웹페이지 즐겨찾기