-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CLOUDSTACK-8677: Call-home functionality for CloudStack #625
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # CloudStack Usage Report | ||
|
|
||
| This directory contains the CloudStack reporter webservice used by the Apache CloudStack project | ||
| to gather anonymous statistical information about CloudStack deployments. | ||
|
|
||
| Since version 4.7 the management server sends out a anonymized Usage Report out to the | ||
| project every 7 days. | ||
|
|
||
| This information is used to gain information about how CloudStack is being used. | ||
|
|
||
| Turning this Usage Reporting functionality off can be done in the Global Settings by setting | ||
| 'usage.report.interval' to 0. | ||
|
|
||
| For more information visit http://cloudstack.apache.org/call-home.html | ||
|
|
||
| # The webservice | ||
| The Python Flask application in this directory is the webservice running on https://reporting.cloudstack.apache.org/report | ||
| and stores all the incoming information in a ElasticSearch database. | ||
|
|
||
| Since Apache CloudStack is Open Source we show not only how we generate the report, but also how we process it. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| #!/usr/bin/env python | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you 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. | ||
|
|
||
| from flask import abort, Flask, request, Response | ||
| from elasticsearch import Elasticsearch | ||
| import json | ||
| import time | ||
|
|
||
| def json_response(response): | ||
| return json.dumps(response, indent=2) + "\n", 200, {'Content-Type': 'application/json; charset=utf-8'} | ||
|
|
||
| def generate_app(config=None): | ||
| app = Flask(__name__) | ||
|
|
||
| @app.route('/report', methods=['POST']) | ||
| def report(): | ||
| # We expect JSON data, so if the Content-Type doesn't match we throw an error | ||
| if 'Content-Type' in request.headers: | ||
| if request.headers['Content-Type'] != 'application/json': | ||
| abort(417, "No or incorrect Content-Type header was supplied") | ||
|
|
||
| index = "cloudstack-%s" % time.strftime("%Y.%m.%d", time.gmtime()) | ||
| timestamp = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()) | ||
|
|
||
| es = Elasticsearch() | ||
| es.indices.create(index=index, ignore=400) | ||
|
|
||
| report = json.loads(request.data) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May be we wanted to add a small check to see report is None and proceed.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, although Flask will catch exceptions and generate HTTP errors based on those |
||
| report["timestamp"] = timestamp | ||
|
|
||
| es.index(index=index, doc_type="usage-report", body=json.dumps(report), | ||
| timestamp=timestamp, refresh=True) | ||
|
|
||
| response = {} | ||
| return json_response(response) | ||
|
|
||
| return app | ||
|
|
||
|
|
||
| app = generate_app() | ||
|
|
||
| # Only run the App if this script is invoked from a Shell | ||
| if __name__ == '__main__': | ||
| app.debug = True | ||
| app.run(host='0.0.0.0', port=8088) | ||
|
|
||
| # Otherwise provide a variable called 'application' for mod_wsgi | ||
| else: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where is this application var used? Its not global var as well?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is for mod_wsgi. It will read "app" and run the application from there |
||
| application = app | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you 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 org.apache.cloudstack.report; | ||
|
|
||
| import com.google.gson.TypeAdapter; | ||
| import com.google.gson.stream.JsonReader; | ||
| import com.google.gson.stream.JsonWriter; | ||
| import com.google.common.util.concurrent.AtomicLongMap; | ||
| import java.util.Map; | ||
| import java.io.IOException; | ||
|
|
||
| public class AtomicGsonAdapter extends TypeAdapter<AtomicLongMap> { | ||
|
|
||
| public AtomicLongMap<Object> read(JsonReader reader) throws IOException { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @wido, |
||
| reader.nextNull(); | ||
| return null; | ||
| } | ||
|
|
||
| public void write(JsonWriter writer, AtomicLongMap value) throws IOException { | ||
| if (value == null) { | ||
| writer.nullValue(); | ||
| return; | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| Map <String, Long> map = value.asMap(); | ||
|
|
||
| writer.beginObject(); | ||
| for (Map.Entry<String, Long> entry : map.entrySet()) { | ||
| writer.name(entry.getKey()).value(entry.getValue()); | ||
| } | ||
| writer.endObject(); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is good work i would say, but just add some comments on this service purpose, may be for people seeing it later can understand it easily. Any reason to have this service written in python and other usage code to run along with cs in java?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This service will not run in the mgmt server. It is where the data is being send. Users will not deploy this, it will be running on call-home.cloudstack.org