diff --git a/docs/conf/prometheus.rst b/docs/conf/prometheus.rst index 07718a4ad..a05081461 100644 --- a/docs/conf/prometheus.rst +++ b/docs/conf/prometheus.rst @@ -43,15 +43,21 @@ In this case, Promgne's pull mode can be used by trigging a small script running or any other job framework. This only requires the server where Prometheus is running, to be able to access Promgen over HTTP. +.. important:: + + Create a Promgen user as a service account with the appropriate permissions, then + generate an API token to access the API endpoints. + .. code-block:: bash #!/bin/sh set -e # Download all the targets from Promgen to a temporary file - curl http://promgen/api/v1/targets --output /etc/prometheus/targets.tmp - # Optionally you could download from a specific service or project - # curl http://promgen/service/123/targets -o /etc/prometheus/targets.tmp - # curl http://promgen/project/456/targets -o /etc/prometheus/targets.tmp + # Permission required: "promgen.read_config" + curl -H "Authorization: Token $PROMGEN_TOKEN" http://promgen/api/v1/targets --output /etc/prometheus/targets.tmp + # Optionally you could download from a specific service or project if you have at least VIEWER permissions on that service or project. + # curl -H "Authorization: Token $PROMGEN_TOKEN" http://promgen/service/123/targets -o /etc/prometheus/targets.tmp + # curl -H "Authorization: Token $PROMGEN_TOKEN" http://promgen/project/456/targets -o /etc/prometheus/targets.tmp # Move our file to make it more atomic mv /etc/prometheus/targets.tmp /etc/prometheus/targets.json # Tell Prometheus to reload diff --git a/docs/user/api.rst b/docs/user/api.rst index 16dba2f44..d5f90592e 100644 --- a/docs/user/api.rst +++ b/docs/user/api.rst @@ -18,6 +18,9 @@ Prometheus nodes. See the `Prometheus API ", views.HostDetail.as_view()), # Prometheus Proxy diff --git a/promgen/views.py b/promgen/views.py index 3d5f5f5bc..7cadb2efa 100644 --- a/promgen/views.py +++ b/promgen/views.py @@ -42,7 +42,6 @@ plugins, prometheus, signals, - tasks, util, ) from promgen.forms import GroupMemberForm, UserPermissionForm @@ -1367,61 +1366,6 @@ def get_check_permission_object(self): return get_object_or_404(models.Farm, id=self.kwargs["pk"]) -class ApiConfig(View): - def get(self, request): - return HttpResponse(prometheus.render_config(), content_type="application/json") - - def post(self, request, *args, **kwargs): - try: - body = json.loads(request.body.decode("utf-8")) - - prometheus.import_config(body, self.request.user, **kwargs) - except Exception as e: - return HttpResponse(e, status=400) - - return HttpResponse("Success", status=202) - - -class ApiQueue(View): - def post(self, request): - signals.trigger_write_config.send(request) - signals.trigger_write_rules.send(request) - signals.trigger_write_urls.send(request) - return HttpResponse("OK", status=202) - - -class _ExportRules(View): - def format(self, rules=None, name="promgen"): - content = prometheus.render_rules(rules) - response = HttpResponse(content) - response["Content-Type"] = "application/x-yaml" - response["Content-Disposition"] = "attachment; filename=%s.rule.yml" % name - return response - - -class RulesConfig(_ExportRules): - def get(self, request): - return self.format() - - -class RuleExport(_ExportRules): - def get(self, request, content_type, object_id): - ct = ContentType.objects.get( - app_label="promgen", model=content_type - ).get_object_for_this_type(pk=object_id) - rules = models.Rule.objects.filter(obj=ct) - return self.format(rules) - - -class URLConfig(View): - def get(self, request): - return HttpResponse(prometheus.render_urls(), content_type="application/json") - - def post(self, request): - tasks.write_urls() - return HttpResponse("OK", status=202) - - class AlertList(LoginRequiredMixin, ListView): paginate_by = 20 queryset = models.Alert.objects.order_by("-created")