80
Icinga Camp | Berlin | 7th of March 2017 Icinga Web 2 How to Write Modules

Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

  • Upload
    icinga

  • View
    417

  • Download
    4

Embed Size (px)

Citation preview

Page 1: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

Icinga Camp | Berlin | 7th of March 2017

Icinga Web 2How to Write Modules

Page 2: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

Icinga Web 2.4.1released on Jan 20, 2017

Page 3: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

• Send commands over the Icinga 2 API

• SSL support for MySQL resources• Export detail views to JSON and

CSV• Change password for DB users

Page 4: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

• Monitoring action bar• Announce banner• SELinux policy• Forked Zend Framework 1

Page 5: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
Page 6: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

How to Write Modules

Page 7: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

• Should I? Why?• Open Source• Great community

Page 8: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

• It’s straight forward• Icinga Web 2 is stable, future-proof

and easy to understand• No deep knowledge of PHP, HTML,

CSS and JS required• So, why not?

Page 9: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

• Lessons learned from Icinga-web 1.x

• Keep framework overhead at a minimum

• Keep it simple• No XML

Page 10: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

/bin/rm

Page 11: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

• Simple• Reliable• Performant

Page 12: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

• Convention over configuration• Put things in the right place rather

than having to configure where things are

• Reasonable defaults• INI config files

Page 13: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

• Zend Framework 1.x• jQuery version 1 and 2• HTMLPurifier, JShrink, Parsedown,

dompdf, lessphp

Page 14: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

• Where to start?• Install Icinga Web 2• Extend the module path

Page 15: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

• Give the module a name• Keep it simple• Reflect what it does• Module name used in PHP

Namespaces, URLs and paths in the file system

Page 16: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

• Create and activate a new module• mkdir -p /usr/share/icingaweb2-

modules/showcase • icingacli module list installed• icingacli module enable showcase

Page 17: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
Page 18: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

• Module meta information in module.info

Page 19: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

CLI Commands

Page 20: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

• Icinga CLI is designed to offer all application logic of Web 2 and its modules also in the CLI

• Helps to provide cronjobs, plugins, tools and even small services

• Bash autocompletion

Page 21: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

• Just create a file with the name of the command in application/clicommands

Page 22: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

application/clicommands/SayCommand.php

Page 23: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
Page 24: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

icingacli <module> <command> <action>

icingacli showcase say something

Page 25: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

• Namespaces help to encapsulate modules

• Every modules has its own namespace built from its name: Icinga\Module\<module>

• The namespace for CLI commands is Clicommands

Page 26: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
Page 27: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

• Document your code• CLI help is generated from

documentation• Get help with --help

Page 28: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
Page 29: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

• Successful actions return exit code 0• Erroneous actions print readable

error messages because all exceptions in CLI are caught

• In this case the exit code is always 1• Get full stacktrace with --trace

Page 30: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
Page 31: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
Page 32: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

CLI Parameter Handling

Page 33: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

• Standalone and named parameters• Default values• Instance of Icinga\Cli\Params

available in $this->params

Page 34: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
Page 35: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
Page 36: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
Page 37: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
Page 38: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

Bring it to the Web

Page 39: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

• MVC• Available actions in controllers• View scripts for output and

presentation• Library code as “model”

Page 40: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

• Every action in a controller is automatically a route in Web 2

icingaweb2/<module>/<controller>/<action>

Page 41: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

• Let‘s create a controller

application/controllers/GinController.php

Page 42: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
Page 43: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

https://…/icingaweb2/showcase/gin/flavors

Page 44: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
Page 45: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

• Create the view script• One directory per controller• One view script per action

application/views/scripts/gin/flavors.phtml

Page 46: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
Page 47: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

• Automatic borders• Scrollable “content”• Fixed “controls” for header

elements

Page 48: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
Page 49: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

Tab Navigation

Page 50: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
Page 51: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
Page 52: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

Menu Entries

Page 53: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

• Users may add their own menu entries

• Modules provide menu entries• configuration.php in the base

directory of the module

Page 54: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
Page 55: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
Page 56: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

Dashboards

Page 57: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

• Create own dashboard• Or extend existing ones• configuration.php

Page 58: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
Page 59: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

• Hide “controls” in dashboards

Page 60: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
Page 61: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

• Enable autorefresh with one single function call in the action that should auto refresh

Page 62: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
Page 63: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

Styles

Page 64: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

• public/css/module.less• LESS extends CSS• Just use CSS if you want• Module styles are isolated

Page 65: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
Page 66: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
Page 67: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
Page 68: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

Feed the View with Data

Page 69: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

• Set data in controllers with$this->view->key = value;

• Use data in views with$this->key or$key

Page 70: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
Page 71: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
Page 72: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

Translation

Page 73: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

• PHP: $this->translate(‘Translate Me’);• icingacli module enable translation• icingacli translation refresh module

shwocase de_DE• Use Poedit• icingacli translation compile module

showcase de_DE

Page 74: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

Configuration

Page 75: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

• /etc/icingaweb2/modules/<module>/

• $this->Config(<file>)• Default file is config.ini• Available in Web and CLI

Page 76: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

[section]key = value

$this->Config()->get(‘key’);

$this->Config()->get(‘nokey’, ‘default-value’)

Page 77: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

Vendor CSS and JS Files

Page 78: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
Page 79: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules
Page 80: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules

www.icinga.orgdev.icinga.orggit.icinga.org

Thank You!@icinga/icinga+icinga