Last active
November 11, 2020 15:40
-
-
Save treydock/476f08a50cb76e47a1047b93f68369be to your computer and use it in GitHub Desktop.
tsm_exporter google/goexpect
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "flag" | |
| "fmt" | |
| "log" | |
| "os" | |
| "regexp" | |
| "time" | |
| expect "github.com/google/goexpect" | |
| ) | |
| var ( | |
| servername = flag.String("servername", "", "TSM servername") | |
| password = flag.String("password", "", "password for dsmadmc") | |
| passRE = regexp.MustCompile("password:") | |
| promptRE = regexp.MustCompile("(?m)Protect: [a-zA-Z0-9]+>") | |
| ) | |
| func main() { | |
| flag.Parse() | |
| os.Setenv("DSM_LOG", "/tmp/test") | |
| cmd := fmt.Sprintf("/usr/bin/dsmadmc -servername=%s -DATAONLY=YES -COMMAdelimited -id=somwell", *servername) | |
| timeout := time.Duration(15) * time.Second | |
| e, _, err := expect.Spawn(cmd, timeout, expect.NoCheck(), expect.Verbose(true), expect.VerboseWriter(os.Stdout)) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| defer e.Close() | |
| content, matches, err := e.Expect(passRE, timeout) | |
| if err != nil { | |
| log.Fatalf("error getting password prompt: %v", err) | |
| } | |
| log.Printf("content1: %s matches1: %v", content, matches) | |
| err = e.Send(*password + "\n") | |
| if err != nil { | |
| log.Fatalf("error sending password: %v", err) | |
| } | |
| content, matches, err = e.Expect(promptRE, timeout) | |
| if err != nil { | |
| log.Fatalf("error getting pre prompt: %v", err) | |
| } | |
| log.Printf("content2: %s matches2: %v", content, matches) | |
| err = e.Send("SELECT DEVCLASS,PCT_UTILIZED,POOLTYPE,STGPOOL_NAME,STG_TYPE FROM stgpools LIMIT 2\n") | |
| if err != nil { | |
| log.Fatalf("error sending command1: %v", err) | |
| } | |
| content, matches, err = e.Expect(regexp.MustCompile("Protect:"), timeout) | |
| if err != nil { | |
| log.Fatalf("error getting post prompt1: %v", err) | |
| } | |
| log.Printf("content3: %s matches3: %v", content, matches) | |
| err = e.Send("SELECT DEVCLASS,PCT_UTILIZED,POOLTYPE,STGPOOL_NAME,STG_TYPE FROM stgpools\n") | |
| if err != nil { | |
| log.Fatalf("error sending command2: %v", err) | |
| } | |
| content, matches, err = e.Expect(regexp.MustCompile("Protect:"), timeout) | |
| if err != nil { | |
| log.Fatalf("error getting post prompt2: %v", err) | |
| } | |
| log.Printf("content4: %s matches4: %v", content, matches) | |
| e.Send("exit\n") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment