@@ -37,6 +37,7 @@ type HMC struct {
3737 ManagedSystemOnly bool
3838 Samples int
3939 TagParsers nmon2influxdblib.TagParsers
40+ Token string
4041}
4142
4243// Point is a struct to simplify InfluxDB point creation
@@ -87,8 +88,7 @@ func NewHMC(c *cli.Context) *HMC {
8788 hmcURL := fmt .Sprintf ("https://" + "%s" + ":12443" , config .HMCServer )
8889 //initialize new http session
8990 hmc .Session = NewSession (config .HMCUser , config .HMCPassword , hmcURL )
90- hmc .Session .doLogon ()
91-
91+ hmc .Token = hmc .Session .doLogon ()
9292 return & hmc
9393}
9494
@@ -235,8 +235,12 @@ func NewSession(user string, password string, url string) *Session {
235235 return & Session {client : & http.Client {Transport : tr , Jar : jar , Timeout : time .Second * timeout }, User : user , Password : password , url : url }
236236}
237237
238+ type Token struct {
239+ API string `xml:"X-API-Session"`
240+ }
241+
238242// doLogon performs the login to the inflxudb instance
239- func (s * Session ) doLogon () {
243+ func (s * Session ) doLogon () ( string ) {
240244
241245 authurl := s .url + "/rest/api/web/Logon"
242246
@@ -274,6 +278,34 @@ func (s *Session) doLogon() {
274278 log .Fatalf ("HMC authentication error: %s\n " , response .Status )
275279 }
276280 }
281+
282+ // store session token
283+ var token Token
284+ bodyBytes , err := ioutil .ReadAll (response .Body )
285+ xml .Unmarshal ([]byte (bodyBytes ), & token )
286+ // log.Printf("%v", token.API)
287+ return token .API
288+ }
289+
290+ func (s * Session ) DoLogoff (token string ) {
291+ authurl := s .url + "/rest/api/web/Logon"
292+ request , err := http .NewRequest ("DELETE" , authurl , nil )
293+
294+ // set request headers
295+ // log.Printf("%v", token)
296+ request .Header .Set ("X-API-Session" , token )
297+ response , err := s .client .Do (request )
298+
299+ if err != nil {
300+ log .Fatalf ("HMC error sending auth request: %v\n " , err )
301+ } else {
302+ defer response .Body .Close ()
303+ if response .StatusCode != 204 {
304+ log .Fatalf ("HMC logoff error: %s\n " , response .Status )
305+ } else {
306+ log .Printf ("Succesfully logged off" )
307+ }
308+ }
277309}
278310
279311// PCMLinks store a system and associated partitions links to PCM data
0 commit comments