From 288670c1b7d9cb55a2831db04556a245965f9680 Mon Sep 17 00:00:00 2001 From: zhaoxiangchun Date: Mon, 17 Feb 2020 11:51:08 +0800 Subject: [PATCH 1/2] =?UTF-8?q?zstack=E7=9B=91=E6=8E=A7=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit monitor.go中GetMonitorData函数通过传入的参数拼装请求参数,然后调用zstack.go中的getMonitor(),通过GET请求,获取相应的指标监控数据 --- pkg/multicloud/zstack/monitor.go | 38 ++++++++++++++++++++++++++++ pkg/multicloud/zstack/zstack.go | 43 ++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 pkg/multicloud/zstack/monitor.go diff --git a/pkg/multicloud/zstack/monitor.go b/pkg/multicloud/zstack/monitor.go new file mode 100644 index 0000000000..52d8b620be --- /dev/null +++ b/pkg/multicloud/zstack/monitor.go @@ -0,0 +1,38 @@ +package zstack + +import ( + "time" + + "yunion.io/x/jsonutils" +) + +type SDataPoint struct { + DataPoints []DataPoint `json:"data"` +} + +type DataPoint struct { + Value float64 `json:"value"` + TimeStemp int64 `json:"time"` + Labels *Label `json:"labels"` +} + +type Label struct { + VMUuid string `json:"VMUuid"` +} + +func (region *SRegion) GetMonitorData(name string, namespace string, since time.Time, + until time.Time) (*SDataPoint, error) { + datas := SDataPoint{} + param := jsonutils.NewDict() + param.Add(jsonutils.NewString(namespace), "namespace") + param.Add(jsonutils.NewString(name), "metricName") + param.Add(jsonutils.NewString("60"), "period") + param.Add(jsonutils.NewInt(since.Unix()), "startTime") + param.Add(jsonutils.NewInt(until.Unix()), "endTime") + rep, err := region.client.getMonitor("zwatch/metrics", param) + if err != nil { + return nil, err + } + rep.Unmarshal(&datas) + return &datas, nil +} diff --git a/pkg/multicloud/zstack/zstack.go b/pkg/multicloud/zstack/zstack.go index 86f81a4d88..884a8a7455 100644 --- a/pkg/multicloud/zstack/zstack.go +++ b/pkg/multicloud/zstack/zstack.go @@ -310,6 +310,49 @@ func (cli *SZStackClient) getResource(resource, resourceId string, retval interf return cloudprovider.ErrDuplicateId } +func (cli *SZStackClient) getMonitor(resource string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) { + + return cli._getMonitor(resource, params) +} + +func (cli *SZStackClient) _getMonitor(resource string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) { + client := httputils.GetDefaultClient() + header := http.Header{} + requestURL := cli.getPostURL(resource) + paramDict := params.(*jsonutils.JSONDict) + if paramDict.Size() > 0 { + values := url.Values{} + for _, key := range paramDict.SortedKeys() { + value, _ := paramDict.GetString(key) + values.Add(key, value) + } + requestURL += fmt.Sprintf("?%s", values.Encode()) + } + var resp jsonutils.JSONObject + startTime := time.Now() + for time.Now().Sub(startTime) < time.Minute*5 { + err := cli.sign(requestURL, "GET", header) + if err != nil { + return nil, err + } + _, resp, err = httputils.JSONRequest(client, context.Background(), "GET", requestURL, header, nil, cli.debug) + if err != nil { + if strings.Contains(err.Error(), "exceeded while awaiting headers") { + time.Sleep(time.Second * 5) + continue + } + return nil, errors.Wrapf(err, fmt.Sprintf("GET %s %s", resource, params)) + } + break + } + + if resp.Contains("location") { + location, _ := resp.GetString("location") + return cli.wait(client, header, "get", requestURL, jsonutils.NewDict(), location) + } + return resp, nil +} + func (cli *SZStackClient) get(resource, resourceId string, spec string) (jsonutils.JSONObject, error) { return cli._get(resource, resourceId, spec) } From 6a54c88d29b4b341e65717b988518c104aec6420 Mon Sep 17 00:00:00 2001 From: zhaoxiangchun Date: Mon, 17 Feb 2020 12:13:16 +0800 Subject: [PATCH 2/2] =?UTF-8?q?gencopyright=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/apis/compute/routetable_const.go | 14 ++++++++++++++ pkg/compute/tasks/ha_disk_create_task.go | 14 ++++++++++++++ pkg/compute/tasks/ha_guest_deploy_task.go | 14 ++++++++++++++ pkg/compute/tasks/ha_guest_start_task.go | 14 ++++++++++++++ pkg/compute/tasks/ha_guest_stop_task.go | 14 ++++++++++++++ pkg/multicloud/aws/route.go | 14 ++++++++++++++ pkg/multicloud/aws/routetable.go | 14 ++++++++++++++ pkg/multicloud/aws/shell/routetable.go | 14 ++++++++++++++ pkg/multicloud/esxi/monitor.go | 14 ++++++++++++++ pkg/multicloud/zstack/monitor.go | 14 ++++++++++++++ pkg/util/iproute2/address.go | 14 ++++++++++++++ pkg/util/iproute2/address_test.go | 14 ++++++++++++++ pkg/util/iproute2/doc.go | 14 ++++++++++++++ pkg/util/iproute2/link.go | 14 ++++++++++++++ pkg/util/iproute2/link_test.go | 14 ++++++++++++++ pkg/util/iproute2/route.go | 14 ++++++++++++++ pkg/util/iproute2/route_test.go | 14 ++++++++++++++ pkg/util/netutils2/netutils_linux.go | 14 ++++++++++++++ pkg/util/netutils2/netutils_others.go | 14 ++++++++++++++ 19 files changed, 266 insertions(+) diff --git a/pkg/apis/compute/routetable_const.go b/pkg/apis/compute/routetable_const.go index c16fdb7f4c..abfcb781d5 100644 --- a/pkg/apis/compute/routetable_const.go +++ b/pkg/apis/compute/routetable_const.go @@ -1,3 +1,17 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package compute const ( diff --git a/pkg/compute/tasks/ha_disk_create_task.go b/pkg/compute/tasks/ha_disk_create_task.go index b465347e67..516af3e718 100644 --- a/pkg/compute/tasks/ha_disk_create_task.go +++ b/pkg/compute/tasks/ha_disk_create_task.go @@ -1,3 +1,17 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package tasks import ( diff --git a/pkg/compute/tasks/ha_guest_deploy_task.go b/pkg/compute/tasks/ha_guest_deploy_task.go index cc1d27e8dd..97ba131778 100644 --- a/pkg/compute/tasks/ha_guest_deploy_task.go +++ b/pkg/compute/tasks/ha_guest_deploy_task.go @@ -1,3 +1,17 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package tasks import ( diff --git a/pkg/compute/tasks/ha_guest_start_task.go b/pkg/compute/tasks/ha_guest_start_task.go index 87b2203943..5c54c33e1e 100644 --- a/pkg/compute/tasks/ha_guest_start_task.go +++ b/pkg/compute/tasks/ha_guest_start_task.go @@ -1,3 +1,17 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package tasks import ( diff --git a/pkg/compute/tasks/ha_guest_stop_task.go b/pkg/compute/tasks/ha_guest_stop_task.go index b1630bda23..cb5a7937ee 100644 --- a/pkg/compute/tasks/ha_guest_stop_task.go +++ b/pkg/compute/tasks/ha_guest_stop_task.go @@ -1,3 +1,17 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package tasks import ( diff --git a/pkg/multicloud/aws/route.go b/pkg/multicloud/aws/route.go index cee5416f22..97a103d740 100644 --- a/pkg/multicloud/aws/route.go +++ b/pkg/multicloud/aws/route.go @@ -1,3 +1,17 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package aws import ( diff --git a/pkg/multicloud/aws/routetable.go b/pkg/multicloud/aws/routetable.go index a8b5245b88..c8fcebc34a 100644 --- a/pkg/multicloud/aws/routetable.go +++ b/pkg/multicloud/aws/routetable.go @@ -1,3 +1,17 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package aws import ( diff --git a/pkg/multicloud/aws/shell/routetable.go b/pkg/multicloud/aws/shell/routetable.go index 30175faee0..1c46f6b6d3 100644 --- a/pkg/multicloud/aws/shell/routetable.go +++ b/pkg/multicloud/aws/shell/routetable.go @@ -1,3 +1,17 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package shell import ( diff --git a/pkg/multicloud/esxi/monitor.go b/pkg/multicloud/esxi/monitor.go index 2f95918e30..79237eb02b 100644 --- a/pkg/multicloud/esxi/monitor.go +++ b/pkg/multicloud/esxi/monitor.go @@ -1,3 +1,17 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package esxi import ( diff --git a/pkg/multicloud/zstack/monitor.go b/pkg/multicloud/zstack/monitor.go index 52d8b620be..5f8d7d9385 100644 --- a/pkg/multicloud/zstack/monitor.go +++ b/pkg/multicloud/zstack/monitor.go @@ -1,3 +1,17 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package zstack import ( diff --git a/pkg/util/iproute2/address.go b/pkg/util/iproute2/address.go index e2c3fda3d1..2c81c80d36 100644 --- a/pkg/util/iproute2/address.go +++ b/pkg/util/iproute2/address.go @@ -1,3 +1,17 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package iproute2 import ( diff --git a/pkg/util/iproute2/address_test.go b/pkg/util/iproute2/address_test.go index f6458d761d..7c36c94f4a 100644 --- a/pkg/util/iproute2/address_test.go +++ b/pkg/util/iproute2/address_test.go @@ -1,3 +1,17 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package iproute2 import ( diff --git a/pkg/util/iproute2/doc.go b/pkg/util/iproute2/doc.go index 3dcd3a17df..ed986fb23e 100644 --- a/pkg/util/iproute2/doc.go +++ b/pkg/util/iproute2/doc.go @@ -1 +1,15 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package iproute2 // import "yunion.io/x/onecloud/pkg/util/iproute2" diff --git a/pkg/util/iproute2/link.go b/pkg/util/iproute2/link.go index 0f357525c6..ffa5f113c3 100644 --- a/pkg/util/iproute2/link.go +++ b/pkg/util/iproute2/link.go @@ -1,3 +1,17 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package iproute2 import ( diff --git a/pkg/util/iproute2/link_test.go b/pkg/util/iproute2/link_test.go index 4bd1b8f910..f35548d9a8 100644 --- a/pkg/util/iproute2/link_test.go +++ b/pkg/util/iproute2/link_test.go @@ -1,3 +1,17 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package iproute2 import ( diff --git a/pkg/util/iproute2/route.go b/pkg/util/iproute2/route.go index dd0dd46ef4..24373beea8 100644 --- a/pkg/util/iproute2/route.go +++ b/pkg/util/iproute2/route.go @@ -1,3 +1,17 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package iproute2 import ( diff --git a/pkg/util/iproute2/route_test.go b/pkg/util/iproute2/route_test.go index 86258a822a..cb4840bb8b 100644 --- a/pkg/util/iproute2/route_test.go +++ b/pkg/util/iproute2/route_test.go @@ -1,3 +1,17 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package iproute2 import ( diff --git a/pkg/util/netutils2/netutils_linux.go b/pkg/util/netutils2/netutils_linux.go index a7e80f13ed..80a1d174d6 100644 --- a/pkg/util/netutils2/netutils_linux.go +++ b/pkg/util/netutils2/netutils_linux.go @@ -1,3 +1,17 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package netutils2 import ( diff --git a/pkg/util/netutils2/netutils_others.go b/pkg/util/netutils2/netutils_others.go index 3be4326842..fcf688dbde 100644 --- a/pkg/util/netutils2/netutils_others.go +++ b/pkg/util/netutils2/netutils_others.go @@ -1,3 +1,17 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + // +build !linux package netutils2