Skip to content

Commit c80f6fb

Browse files
committed
Add os-capacity prometheus
1 parent 938669c commit c80f6fb

File tree

5 files changed

+29
-34
lines changed

5 files changed

+29
-34
lines changed

README.rst

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -135,23 +135,8 @@ See the online help for more details:
135135
complete print bash completion command
136136
flavor list List all the flavors.
137137
help print detailed help for another command
138+
prometheus To be run as node exporter textfile collector
138139
resources all List all resource providers, with their resources and servers.
139140
resources group Lists counts of resource providers with similar inventories.
140141
usages all List all current resource usages.
141142
usages group Group usage by specified key (by user or project).
142-
143-
Submitting Metrics to Monasca
144-
-----------------------------
145-
146-
There is now an experimental mode where metrics can be written into Monasca
147-
for the calls "resources group" and "usages group user". To enable this
148-
feature you must set::
149-
150-
export OS_CAPACITY_SEND_METRICS=1
151-
152-
To later disable the feature you must unset the environment variable::
153-
154-
unset OS_CAPACITY_SEND_METRICS
155-
156-
For an example of using this with cron, please see the example script in
157-
``cron/example.sh``.

os_capacity/commands/commands.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@
1919

2020
from os_capacity.data import metrics
2121
from os_capacity import utils
22+
from os_capacity.data import candidates
23+
24+
25+
class PrometheusAll(Lister):
26+
"""To be run as node exporter textfile collector."""
27+
def take_action(self, parsed_args):
28+
candidates.print_exporter_data(self.app)
29+
# TODO(johngarbutt) a total hack!
30+
return (('fake'), [])
2231

2332

2433
class FlavorList(Lister):

os_capacity/data/candidates.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
import openstack
77

88

9-
def get_capacity_per_flavor(conn, flavors):
9+
def get_capacity_per_flavor(placement_client, flavors):
1010
capacity_per_flavor = {}
1111

1212
for flavor in flavors:
1313
resources, traits = get_placement_request(flavor)
14-
max_per_host = get_max_per_host(conn, resources, traits)
14+
max_per_host = get_max_per_host(placement_client, resources, traits)
1515
capacity_per_flavor[flavor.name] = max_per_host
1616

1717
return capacity_per_flavor
@@ -27,20 +27,22 @@ def get_placement_request(flavor):
2727
if "resources:" == key[:10]:
2828
count = int(value)
2929
resources[key[10:]] = count
30+
if "hw:cpu_policy" == key and value == "dedicated":
31+
resources["PCPU"] = flavor.vcpus
3032
if "PCPU" not in resources.keys() and "VCPU" not in resources.keys():
3133
resources["VCPU"] = flavor.vcpus
3234
return resources, required_traits
3335

3436

35-
def get_max_per_host(conn, resources, required_traits):
37+
def get_max_per_host(placement_client, resources, required_traits):
3638
resource_str = ",".join(
3739
[key + ":" + str(value) for key, value in resources.items() if value]
3840
)
3941
required_str = ",".join(required_traits)
4042
# TODO(johngarbut): remove disabled!
4143
forbidden_str = "COMPUTE_STATUS_DISABLED"
4244

43-
response = conn.placement.get(
45+
response = placement_client.get(
4446
"/allocation_candidates",
4547
params={"resources": resource_str, "required": required_str},
4648
headers={"OpenStack-API-Version": "placement 1.29"},
@@ -63,11 +65,9 @@ def get_max_per_host(conn, resources, required_traits):
6365
return count_per_rp
6466

6567

66-
def main():
67-
conn = openstack.connect()
68-
69-
flavors = list(conn.compute.flavors())
70-
capacity_per_flavor = get_capacity_per_flavor(conn, flavors)
68+
def print_exporter_data(app):
69+
flavors = list(app.compute_client.flavors())
70+
capacity_per_flavor = get_capacity_per_flavor(app.placement_client, flavors)
7171

7272
# total capacity per flavor
7373
flavor_names = sorted([f.name for f in flavors])
@@ -77,17 +77,16 @@ def main():
7777
print(f'openstack_total_capacity_per_flavor{{flavor="{flavor_name}"}} {total}')
7878

7979
# capacity per host
80-
raw_rps = list(conn.placement.resource_providers())
80+
raw_rps = list(app.placement_client.resource_providers())
8181
resource_providers = {rp.name: rp.id for rp in raw_rps}
8282
hostnames = sorted(resource_providers.keys())
8383
for hostname in hostnames:
8484
rp_id = resource_providers[hostname]
8585
for flavor_name in flavor_names:
8686
all_counts = capacity_per_flavor.get(flavor_name, {})
8787
our_count = all_counts.get(rp_id, 0)
88+
if our_count == 0:
89+
continue
8890
print(
8991
f'openstack_capacity_by_hostname{{hypervisor="{hostname}",flavor="{flavor_name}"}} {our_count}'
9092
)
91-
92-
93-
main()

os_capacity/shell.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from cliff.commandmanager import CommandManager
1919
import os_client_config
2020

21+
import openstack
2122

2223
def get_cloud_config():
2324
# TODO(johngarbutt) consider passing in argument parser
@@ -41,11 +42,11 @@ def __init__(self):
4142
def initialize_app(self, argv):
4243
self.LOG.debug('initialize_app')
4344

44-
config = os_client_config.get_config()
45-
self.compute_client = config.get_session_client("compute")
46-
self.placement_client = config.get_session_client("placement")
47-
self.monitoring_client = config.get_session_client("monitoring")
48-
self.identity_client = config.get_session_client("identity")
45+
conn = openstack.openstack.connect()
46+
self.connection = conn
47+
self.compute_client = conn.compute
48+
self.placement_client = conn.placement
49+
self.identity_client = conn.identity
4950

5051
self.LOG.debug('setup Keystone API REST clients')
5152

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
'.commands:ListResourcesGroups',
5858
'usages_all = os_capacity.commands.commands:ListUsagesAll',
5959
'usages_group = os_capacity.commands.commands:ListUsagesGroup',
60+
'prometheus = os_capacity.commands.commands:PrometheusAll',
6061
],
6162
},
6263
)

0 commit comments

Comments
 (0)