1 #######################################################################################
2 # Copyright ETSI Contributors and Others.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #######################################################################################
17 from datetime
import datetime
, timedelta
19 from airflow
import DAG
20 from airflow
.decorators
import task
21 from osm_mon
.core
.common_db
import CommonDbClient
22 from osm_mon
.core
.config
import Config
23 from osm_mon
.vim_connectors
.azure
import AzureCollector
24 from osm_mon
.vim_connectors
.gcp
import GcpCollector
25 from osm_mon
.vim_connectors
.openstack
import OpenStackCollector
26 from prometheus_client
import CollectorRegistry
, Gauge
, push_to_gateway
29 SUPPORTED_VIM_TYPES
= ["openstack", "vio", "gcp", "azure"]
30 PROMETHEUS_PUSHGW
= "pushgateway-prometheus-pushgateway:9091"
31 PROMETHEUS_JOB_PREFIX
= "airflow_osm_vim_status_"
32 PROMETHEUS_METRIC
= "vim_status"
33 PROMETHEUS_METRIC_DESCRIPTION
= "VIM status"
38 """Get VIMs from MongoDB"""
39 print("Getting VIM list")
43 common_db
= CommonDbClient(cfg
)
44 vim_accounts
= common_db
.get_vim_accounts()
46 for vim
in vim_accounts
:
47 print(f
'Read VIM {vim["_id"]} ({vim["name"]})')
49 {"_id": vim
["_id"], "name": vim
["name"], "vim_type": vim
["vim_type"]}
53 print("Getting VIM list OK")
57 def create_dag(dag_id
, dag_number
, dag_description
, vim_id
):
62 "depends_on_past": False,
64 # "retry_delay": timedelta(minutes=1),
65 "retry_delay": timedelta(seconds
=10),
67 description
=dag_description
,
68 is_paused_upon_creation
=False,
69 # schedule_interval=timedelta(minutes=SCHEDULE_INTERVAL),
70 schedule_interval
=f
"*/{SCHEDULE_INTERVAL} * * * *",
71 start_date
=datetime(2022, 1, 1),
77 def get_vim_collector(vim_account
):
78 """Return a VIM collector for the vim_account"""
79 vim_type
= vim_account
["vim_type"]
80 if "config" in vim_account
and "vim_type" in vim_account
["config"]:
81 vim_type
= vim_account
["config"]["vim_type"].lower()
82 if vim_type
== "vio" and "vrops_site" not in vim_account
["config"]:
83 vim_type
= "openstack"
84 if vim_type
== "openstack":
85 return OpenStackCollector(vim_account
)
87 return GcpCollector(vim_account
)
88 if vim_type
== "azure":
89 return AzureCollector(vim_account
)
90 print(f
"VIM type '{vim_type}' not supported")
93 @task(task_id
="get_vim_status_and_send_to_prometheus")
94 def get_vim_status_and_send_to_prometheus(vim_id
: str):
95 """Authenticate against VIM and check status"""
97 # Get VIM account info from MongoDB
98 print(f
"Reading VIM info, id: {vim_id}")
100 common_db
= CommonDbClient(cfg
)
101 vim_account
= common_db
.get_vim_account(vim_account_id
=vim_id
)
104 # Define Prometheus Metric for NS topology
105 registry
= CollectorRegistry()
108 PROMETHEUS_METRIC_DESCRIPTION
,
114 metric
.labels(vim_id
).set(0)
117 collector
= get_vim_collector(vim_account
)
119 status
= collector
.is_vim_ok()
120 print(f
"VIM status: {status}")
121 metric
.labels(vim_id
).set(1)
123 print("Error creating VIM collector")
126 gateway
=PROMETHEUS_PUSHGW
,
127 job
=f
"{PROMETHEUS_JOB_PREFIX}{vim_id}",
132 get_vim_status_and_send_to_prometheus(vim_id
)
137 vim_list
= get_all_vim()
138 for index
, vim
in enumerate(vim_list
):
139 vim_type
= vim
["vim_type"]
140 if vim_type
in SUPPORTED_VIM_TYPES
:
142 vim_name
= vim
["name"]
143 dag_description
= f
"Dag for VIM {vim_name} status"
144 dag_id
= f
"vim_status_{vim_id}"
145 print(f
"Creating DAG {dag_id}")
146 globals()[dag_id
] = create_dag(
149 dag_description
=dag_description
,
153 print(f
"VIM type '{vim_type}' not supported for monitoring VIM status")