package main import ( "flag" "fmt" "git.mlody.eu/lmoskala/libguestd/pkg/libguestd" "github.com/digitalocean/go-libvirt" "log" "net/url" "time" ) func main() { libvirtUri := flag.String("libvirt", "qemu:///system", "libvirt connection URI") domainName := flag.String("domain", "", "domain name") GetOSInfo := flag.Bool("osinfo", false, "Gather OS info") GetNICInfo := flag.Bool("nicinfo", false, "Gather NIC info") GetFSInfo := flag.Bool("fsinfo", false, "Gather Filesystem info") ListKeys := flag.Bool("listkeys", false, "List ssh keys (specify username)") GetTimeDelta := flag.Bool("timedelta", false, "Display time delta between this host and guest") GetHostName := flag.Bool("hostname", false, "Display host name") GetAgentInfo := flag.Bool("agentinfo", false, "Display agent info") GetDiskInfo := flag.Bool("diskinfo", false, "Display disk info") GetDiskStats := flag.Bool("diskstats", false, "Display disk I/O stats") Username := flag.String("username", "", "username to edit (use with password or ssh keys)") Password := flag.String("password", "", "password to set (specify username)") AddKey := flag.String("sshkey-add", "", "SSH key to add (in authorized_keys format), specify username") RemoveKey := flag.String("sshkey-remove", "", "SSH key to remove (in authorized_keys format), specify username") flag.Parse() uri, _ := url.Parse(*libvirtUri) l, err := libvirt.ConnectToURI(uri) if err != nil { log.Fatalf("failed to connect: %v", err) } domains, _, err := l.ConnectListAllDomains(1, libvirt.ConnectListDomainsActive) if err != nil { log.Fatalf("failed to list all domains: %v", err) } for _, domain := range domains { if domain.Name != *domainName { continue } err = libguestd.GuestPing(l, domain) if err != nil { log.Printf("Error(GuestPing): %s", err) continue } agentInfo, err := libguestd.GuestInfo(l, domain) if err != nil { log.Printf("Error(GuestInfo): %s", err) continue } if *GetAgentInfo { agentInfo.Dump() } if *GetOSInfo { osInfo, err := libguestd.GuestGetOsInfo(l, domain, agentInfo) if err != nil { log.Printf("Error(GuestGetOsInfo): %s", err) } else { osInfo.Dump() } } if *GetNICInfo { interfaces, err := libguestd.GuestGetNetworkInterfaces(l, domain, agentInfo) if err != nil { log.Printf("Error(GuestGetNetworkInterfaces): %s", err) } else { for _, iface := range interfaces { iface.Dump() } } } if *GetFSInfo { fs, err := libguestd.GuestGetFilesystemInfo(l, domain, agentInfo) if err != nil { log.Printf("Error(GuestGetFilesystemInfo): %s", err) } else { for _, fs := range fs { fs.Dump() } } } if *GetDiskInfo { disk, err := libguestd.GuestGetDisks(l, domain, agentInfo) if err != nil { log.Printf("Error(GuestGetDisks): %s", err) } else { for _, disk := range disk { disk.Dump() } } } if *GetDiskStats { diskstats, err := libguestd.GuestGetDiskstats(l, domain, agentInfo) if err != nil { log.Printf("Error(GuestGetDiskstats): %s", err) } else { for _, disk := range diskstats { disk.Dump() } } } if *Username != "" && *Password != "" { err = libguestd.GuestChangeUserPassword(l, domain, agentInfo, *Username, *Password) if err != nil { log.Printf("Error(GuestChangeUserPassword): %s", err) } else { log.Printf("Password set") } } if *Username != "" && *AddKey != "" { err = libguestd.GuestAddSshKeys(l, domain, agentInfo, *Username, []string{*AddKey}, false) if err != nil { log.Printf("Error(GuestAddSshKeys): %s", err) } else { log.Printf("SSH key added") } } if *Username != "" && *RemoveKey != "" { err = libguestd.GuestRemoveSshKeys(l, domain, agentInfo, *Username, []string{*RemoveKey}) if err != nil { log.Printf("Error(GuestRemoveSshKeys): %s", err) } else { log.Printf("SSH key removed") } } if *Username != "" && *ListKeys { keys, err := libguestd.GuestGetSshKeys(l, domain, agentInfo, *Username) if err != nil { log.Printf("Error(GuestGetSshKeys): %s", err) } fmt.Printf("Keys for %s:\n", *Username) for _, key := range keys { fmt.Printf("\t%s\n", key) } } if *GetHostName { hostname, err := libguestd.GuestGetHostName(l, domain, agentInfo) if err != nil { log.Printf("Error(GuestGetHostName): %s", err) } else { fmt.Printf("Hostname: %s\n", hostname) } } if *GetTimeDelta { guestTime, err := libguestd.GuestGetTime(l, domain, agentInfo) if err != nil { log.Printf("Error(GuestGetTime): %s", err) } else { d := time.Since(time.Unix(0, guestTime)) fmt.Printf("Time diff: %f seconds\n", d.Seconds()) } } } if err = l.Disconnect(); err != nil { log.Fatalf("failed to disconnect: %v", err) } }