-
Notifications
You must be signed in to change notification settings - Fork 5
Grafana #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Grafana #30
Changes from 13 commits
0853623
93a33da
fea55ff
7a4ffc1
79ae9ff
5c52174
0d05cf7
bfbdde3
0d40dea
84bc0e6
62184c1
e9db1f9
f239894
8b669ba
d2662c8
85cfca3
b5c3595
bf28eef
52d76ac
a7a7434
6751c64
96b5a55
2618337
36848ed
84d3ba3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| FROM grafana/grafana:latest | ||
|
||
|
|
||
| USER root | ||
|
|
||
| ENV GF_INSTALL_PLUGINS=alexanderzobnin-zabbix-app | ||
|
|
||
| RUN chown -R grafana:grafana /etc/grafana | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,8 @@ | |
| # | ||
|
|
||
| version: "3" | ||
|
|
||
|
|
||
| services: | ||
|
|
||
| server: | ||
|
|
@@ -48,6 +50,8 @@ services: | |
| - MYSQL_DATABASE=${MYSQL_DATABASE:-zabbix} | ||
| - MYSQL_USER=${MYSQL_USER:-zabbix} | ||
| - MYSQL_PASSWORD=${MYSQL_PASSWORD:-zabbix} | ||
| volumes: | ||
| - ./scripts/nginx.conf:/etc/zabbix/nginx.conf:ro | ||
| restart: unless-stopped | ||
| logging: | ||
| driver: "json-file" | ||
|
|
@@ -88,8 +92,34 @@ services: | |
| max-size: "10M" | ||
| max-file: "5" | ||
|
|
||
| grafana: | ||
| hostname: grafana | ||
| env_file: | ||
| - .env | ||
| build: | ||
| dockerfile: Dockerfile.grafana | ||
| context: ./ | ||
| restart: always | ||
|
||
| networks: | ||
| - zabbix_net | ||
| volumes: | ||
| - /data/grafana:/var/lib/grafana | ||
|
||
| - /data/provisioning/dashboards:/etc/grafana/provisioning/dashboards | ||
| - /data/provisioning/datasources:/etc/grafana/provisioning/datasources | ||
| - ./scripts/zabbix_dashboards:/var/lib/grafana/dashboards | ||
| - ./scripts/grafana.ini:/etc/grafana/grafana.ini:ro | ||
|
||
| - ./scripts/ldap.toml:/etc/grafana/ldap.toml:ro | ||
| restart: unless-stopped | ||
|
||
| logging: | ||
| driver: "json-file" | ||
| options: | ||
| max-size: "10M" | ||
| max-file: "5" | ||
|
|
||
| configurator: | ||
| hostname: configuration | ||
| env_file: | ||
| - .env | ||
| build: | ||
| dockerfile: Dockerfile.configurator | ||
| context: ./ | ||
|
|
@@ -98,6 +128,9 @@ services: | |
| - ./scripts/configurator.py:/configurator.py:ro | ||
| - ./configuration:/configuration:ro | ||
| - /etc/zabbix:/etc/zabbix | ||
| - /data/provisioning/dashboards:/grafana/provisioning/dashboards | ||
| - /data/provisioning/datasources:/grafana/provisioning/datasources | ||
| - ./scripts/zabbix_dashboards:/grafana/zabbix_dashboards | ||
| links: | ||
| - db | ||
| - frontend | ||
|
|
@@ -129,6 +162,12 @@ services: | |
| - ZBX_CUSTOM_CONFIG=custom.json | ||
| # Usage: ZBX_ADDITIONAL_TEMPLATES=template1,template2 | ||
| - ZBX_ADDITIONAL_TEMPLATES=Template Web Check, Template App Docker | ||
| #GRAFANA ENVIRONMENTS | ||
| - GRA_DSOURCE_YAML=/grafana/provisioning/datasources/datasource.yaml | ||
| - GRA_DBOARD_YAML=/grafana/provisioning/dashboards/dashboard.yaml | ||
| - GRA_ADMIN_PASS=${GF_SECURITY_ADMIN_PASSWORD} | ||
| - GRA_PATH_TO_DASHBOARDS=/grafana/zabbix_dashboards | ||
| - GRA_HOST=grafana | ||
| networks: | ||
| zabbix_net: | ||
| driver: bridge | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,7 @@ Requirements | |
|
|
||
| ### Preparing configuration file for server | ||
|
|
||
| If default options are not enough, then please use `.env.example` to create own environment configuration file. | ||
| Tou need to create configuration file `.env` from `.env.example` | ||
|
||
|
|
||
| ### Run Zabbix server | ||
|
|
||
|
|
@@ -33,7 +33,7 @@ Zabbix agent on host machine, where Zabbix server works inside the docker enviro | |
|
|
||
| #### Run and configured only server components | ||
| ```shell | ||
| $ docker-compose up -d | ||
| $ docker-compose up -d && docker restart $(docker ps -q) | ||
|
||
| ``` | ||
| Zabbix provisioned service is **configurator**, after succesfully configuring the server, configurator has to return exit code 0. | ||
| To be sure that it is precisely so, execute such command and look at state column: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,11 @@ def error(msg=""): | |
|
|
||
| class Configurator: | ||
| def __init__(self): | ||
| self.grafana_password = os.environ["GF_SECURITY_ADMIN_PASSWORD"] | ||
| self.grafana_datas_yaml = os.environ["GRA_DSOURCE_YAML"] | ||
| self.grafana_dashb_yaml = os.environ["GRA_DBOARD_YAML"] | ||
| self.grafana_hostname = os.environ["GRA_HOST"] | ||
| self.grafana_pt_dashboards = os.environ["GRA_PATH_TO_DASHBOARDS"] | ||
| self.url = os.environ["ZBX_SERVER_URL"] | ||
| self.server_host = os.environ["ZBX_SERVER_HOST"] | ||
| self.default_admin_username = "admin" | ||
|
|
@@ -95,6 +100,85 @@ def __init__(self): | |
| self.admin_users = json.loads(os.environ["ZBX_ADMIN_USERS"]) if "ZBX_ADMIN_USERS" in os.environ and os.environ["ZBX_ADMIN_USERS"].strip() != "" else [] | ||
| self.additional_templates = [x.strip() for x in os.environ["ZBX_ADDITIONAL_TEMPLATES"].split(",")] if "ZBX_ADDITIONAL_TEMPLATES" in os.environ else [] | ||
|
|
||
| def grafana_plugin_on(self): | ||
| logger.debug("Grafana Plugin Curl Start") | ||
| plugURL = ("{0}:3000/api/plugins/alexanderzobnin-zabbix-app/settings?enabled=true".format(self.grafana_hostname)) | ||
| c = pycurl.Curl() | ||
| c.setopt(c.USERPWD, "%s:%s" % ("admin", self.grafana_password)) | ||
| c.setopt(c.URL, plugURL) | ||
| c.setopt(c.POSTFIELDS, '{ '' }') | ||
| c.setopt(c.VERBOSE, True) | ||
| c.perform() | ||
|
|
||
| def grafana_dashboard_starred(self): | ||
| logger.debug("Grafana Dashboard Starred") | ||
| path, dirs, files = os.walk(self.grafana_pt_dashboards).next() | ||
| file_count = len(files) | ||
| print (file_count) | ||
|
|
||
| dashboard_id = 1 | ||
| #Condition for find more then one dashboard.json file (need for add to favorite) | ||
| while dashboard_id <= file_count: | ||
| #print (dashboard_id) | ||
| plugURL = ("{0}:3000/api/user/stars/dashboard/{1}".format(self.grafana_hostname, dashboard_id)) | ||
| c = pycurl.Curl() | ||
| c.setopt(c.USERPWD, "%s:%s" % ("admin", self.grafana_password)) | ||
| c.setopt(c.URL, plugURL) | ||
| c.setopt(c.POSTFIELDS, '{ '' }') | ||
| c.setopt(c.VERBOSE, True) | ||
| c.perform() | ||
| dashboard_id = dashboard_id + 1 #So simply :) | ||
|
|
||
| def grafana_configurator(self): | ||
| #DATASOURCE.YAML | ||
| yaml_file_ds = open(self.grafana_datas_yaml, 'w') | ||
|
||
| yaml_file_ds.write('apiVersion: 1\n\n') | ||
| yaml_file_ds.write('datasources:\n') | ||
| yaml_file_ds.write('- name: Zabbix\n') | ||
| yaml_file_ds.write(' type: alexanderzobnin-zabbix-datasource\n') | ||
| yaml_file_ds.write(' access: proxy\n') | ||
| yaml_file_ds.write(' url: {}/api_jsonrpc.php\n'.format(self.url)) | ||
| yaml_file_ds.write(' isDefault: true\n') | ||
| yaml_file_ds.write(' jsonData:\n') | ||
| yaml_file_ds.write(' username: admin\n') | ||
| yaml_file_ds.write(' password: {0}\n'.format(self.admin_password)) | ||
| yaml_file_ds.write(' trends: true\n') | ||
| yaml_file_ds.write(' trendsFrom: 7d\n') | ||
| yaml_file_ds.write(' trendsRange: 4d\n') | ||
| yaml_file_ds.write(' cacheTTL: 1h\n') | ||
| yaml_file_ds.write(' alerting: true\n') | ||
| yaml_file_ds.write(' version: 1\n') | ||
| yaml_file_ds.write(' editable: false\n') | ||
| yaml_file_ds.close() | ||
|
|
||
| #DASHBOARD.YAML | ||
| yaml_file_db = open(self.grafana_dashb_yaml, 'w') | ||
| yaml_file_db.write('apiVersion: 1\n\n') | ||
|
||
| yaml_file_db.write('providers:\n') | ||
| yaml_file_db.write(' - name: Zabbix\n') | ||
| yaml_file_db.write(' folder:\n') | ||
| yaml_file_db.write(' folderUid: \n') | ||
| yaml_file_db.write(' type: file\n') | ||
| yaml_file_db.write(' updateIntervalSeconds: 60\n') | ||
| yaml_file_db.write(' options:\n') | ||
| yaml_file_db.write(' path: /var/lib/grafana/dashboards') | ||
| yaml_file_db.close() | ||
| #Grafana Port Checker (need for check, if server start?) | ||
| while True: | ||
| time.sleep(1) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Сколько ты будешь ждать, пока графана заработает?, Я так понимаю тут бесконечный цикл. Правильно?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. не бесконечный цикл, тут идет порт чек. как только графана доступна цикл завершается. Иногда бывало так , что графана загружалась позже выполнения скриптов и выдавало ошибку. Допишу кое что, что бы в случаи если графана не подымится, цикл закончился через какое то время)) |
||
| sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
|
||
| result = sock.connect_ex((self.grafana_hostname,3000)) | ||
| if result == 0: | ||
| logger.debug("Grafana Server is Started") | ||
| self.grafana_plugin_on() #Enable Plugin | ||
| self.grafana_dashboard_starred() #Add all dashboards to favorite | ||
| sock.close() | ||
|
||
| break | ||
| else: | ||
| logger.debug("Grafana Server still not start, Try...") | ||
| sock.close() | ||
|
|
||
|
|
||
| def login(self): | ||
| logger.debug("Login into Zabbix server (%s)."%(self.url)) | ||
| login_error = False | ||
|
|
@@ -580,7 +664,7 @@ def assign_template(self, host_id, template_name): | |
| return 1 | ||
|
|
||
| def main(self): | ||
|
|
||
| self.grafana_configurator() | ||
| if self.authentication_type != self.default_authentication_type: | ||
| logger.debug("Changing authentication_type to default to use api with basic credentials.") | ||
| self.update_configuration(config={"authentication_type":self.default_authentication_type}) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
я понял, почему так сделано, но docker-compose.yml должен быть разработанный таким образом, чтобы .env не был обязательным. Он должен быть опциональным.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
в .env файле хранится пароль от графаны, который тянется в саму графану и в конфигуратор. Мне показалось что вариант хранения пароля в docker-compose.yml неправильный. Но только что я придумал ещё один вариант реализации... хм... Позже внесу его.