Hello, everyone!
I’m working on a Low-Level Discovery (LLD) script in Zabbix that’s set up as an "External" item. The script interacts with Aruba’s API to generate hosts for different clients based on their specific sites and devices.
The challenge I’m facing is related to API token management. The Aruba API requires authentication via access tokens, and these tokens expire after a certain time. When a token expires, I need to refresh it using a refresh token, which the API provides.
My Setup:
- I store the API credentials, including the client ID, client secret, access token, and refresh token for multiple clients in a shared `.ini` file.
- The script is executed simultaneously for multiple clients, each accessing their own section in the `.ini` file. Each execution refreshes the token if it’s expired, and updates the file with the new token.
The Problem:
When I run the script concurrently (for example, for 5 different clients at the same time), the tokens in the `.ini` file are not updated correctly. This causes token values to get lost or overwritten, leading to issues in API authentication.
Here’s what I’ve tried:
1. I implemented file locking using `fcntl` to prevent multiple processes from writing to the file at the same time.
2. Each client’s tokens are stored in a separate section of the `.ini` file, and each script execution is responsible for updating its own client’s section.
3. Despite using file locks, the tokens still sometimes get lost when the script is run concurrently for different clients.
My Question:
Has anyone come across a similar issue when managing API tokens for multiple clients simultaneously in Zabbix, or when working with concurrent updates to a shared data source (like an `.ini` file)?
Is there a better way to handle token refreshing in this scenario to avoid data loss or corruption?
Any suggestions on how to manage this would be greatly appreciated!
Source Code:
For anyone interested in taking a look at the source code, you can find it at my GitHub repository: https://github.com/HorselessName/monitoramento-aruba
Thanks in advance for your help!
I’m working on a Low-Level Discovery (LLD) script in Zabbix that’s set up as an "External" item. The script interacts with Aruba’s API to generate hosts for different clients based on their specific sites and devices.
The challenge I’m facing is related to API token management. The Aruba API requires authentication via access tokens, and these tokens expire after a certain time. When a token expires, I need to refresh it using a refresh token, which the API provides.
My Setup:
- I store the API credentials, including the client ID, client secret, access token, and refresh token for multiple clients in a shared `.ini` file.
- The script is executed simultaneously for multiple clients, each accessing their own section in the `.ini` file. Each execution refreshes the token if it’s expired, and updates the file with the new token.
The Problem:
When I run the script concurrently (for example, for 5 different clients at the same time), the tokens in the `.ini` file are not updated correctly. This causes token values to get lost or overwritten, leading to issues in API authentication.
Here’s what I’ve tried:
1. I implemented file locking using `fcntl` to prevent multiple processes from writing to the file at the same time.
2. Each client’s tokens are stored in a separate section of the `.ini` file, and each script execution is responsible for updating its own client’s section.
3. Despite using file locks, the tokens still sometimes get lost when the script is run concurrently for different clients.
My Question:
Has anyone come across a similar issue when managing API tokens for multiple clients simultaneously in Zabbix, or when working with concurrent updates to a shared data source (like an `.ini` file)?
Is there a better way to handle token refreshing in this scenario to avoid data loss or corruption?
Any suggestions on how to manage this would be greatly appreciated!
Source Code:
For anyone interested in taking a look at the source code, you can find it at my GitHub repository: https://github.com/HorselessName/monitoramento-aruba
Thanks in advance for your help!