-
What Grafana version and what operating system are you using?
10.4.3 -
What are you trying to achieve?
Check the user existence in Grafana using login/email in python scripting -
How are you trying to achieve it?
def authenticate_admin(admin_username, admin_password, grafana_url):
login_url = f"{grafana_url}/login"
data = {‘user’: admin_username, ‘password’: admin_password}
response = requests.post(login_url, data=data)
if response.status_code == 200:
return response.cookies.get(‘grafana_sess’)
else:
print(“Failed to authenticate admin.”)
return None
def check_user_existence(username, grafana_url, admin_username, admin_password):
session_cookie = authenticate_admin(admin_username, admin_password, grafana_url)
if session_cookie:
headers = {‘Cookie’: ‘grafana_sess=’ + session_cookie}
users_url = f"{grafana_url}/api/users/search"
response = requests.get(users_url, headers=headers, params={‘query’: username})
if response.status_code == 200:
users = response.json()
for user in users:
if user[‘login’] == username or user[‘email’].startswith(username):
return True
return False
-
What happened?
Failed to authenticate admin. -
What did you expect to happen?
admin login pass, followed by print user existence in Grafana. -
Can you copy/paste the configuration(s) that you are having problems with?
def authenticate_admin(admin_username, admin_password, grafana_url):
login_url = f"{grafana_url}/login"
data = {‘user’: admin_username, ‘password’: admin_password}
response = requests.post(login_url, data=data)
if response.status_code == 200:
return response.cookies.get(‘grafana_sess’)
else:
print(“Failed to authenticate admin.”)
return None
def check_user_existence(username, grafana_url, admin_username, admin_password):
session_cookie = authenticate_admin(admin_username, admin_password, grafana_url)
if session_cookie:
headers = {‘Cookie’: ‘grafana_sess=’ + session_cookie}
users_url = f"{grafana_url}/api/users/search"
response = requests.get(users_url, headers=headers, params={‘query’: username})
if response.status_code == 200:
users = response.json()
for user in users:
if user[‘login’] == username or user[‘email’].startswith(username):
return True
return False
def main():
grafana_url = “http://localhost:3000”
admin_username = “admin”
admin_password = “************”
# Example usage to check user existence
username = "ABC"
if check_user_existence(username, grafana_url, admin_username, admin_password):
print(f"User with username '{username}' exists in Grafana.")
else:
print(f"User with username '{username}' does not exist in Grafana.")
if name == “main”:
main()
-
Did you receive any errors in the Grafana UI or in related logs? If so, please tell us exactly what they were.
-
Did you follow any online instructions? If so, what is the URL?