icinga camp berlin 2017 - icinga web 2 - how to write modules
TRANSCRIPT
![Page 1: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/1.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/2.jpg)
Icinga Web 2.4.1released on Jan 20, 2017
![Page 3: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/3.jpg)
• 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](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/4.jpg)
• Monitoring action bar• Announce banner• SELinux policy• Forked Zend Framework 1
![Page 5: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/5.jpg)
![Page 6: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/6.jpg)
How to Write Modules
![Page 7: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/7.jpg)
• Should I? Why?• Open Source• Great community
![Page 8: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/8.jpg)
• 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](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/9.jpg)
• 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](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/10.jpg)
/bin/rm
![Page 11: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/11.jpg)
• Simple• Reliable• Performant
![Page 12: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/12.jpg)
• 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](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/13.jpg)
• 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](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/14.jpg)
• Where to start?• Install Icinga Web 2• Extend the module path
![Page 15: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/15.jpg)
• 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](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/16.jpg)
• 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](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/17.jpg)
![Page 18: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/18.jpg)
• Module meta information in module.info
![Page 19: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/19.jpg)
CLI Commands
![Page 20: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/20.jpg)
• 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](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/21.jpg)
• 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](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/22.jpg)
application/clicommands/SayCommand.php
![Page 23: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/23.jpg)
![Page 24: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/24.jpg)
icingacli <module> <command> <action>
icingacli showcase say something
![Page 25: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/25.jpg)
• 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](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/26.jpg)
![Page 27: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/27.jpg)
• 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](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/28.jpg)
![Page 29: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/29.jpg)
• 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](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/30.jpg)
![Page 31: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/31.jpg)
![Page 32: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/32.jpg)
CLI Parameter Handling
![Page 33: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/33.jpg)
• 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](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/34.jpg)
![Page 35: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/35.jpg)
![Page 36: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/36.jpg)
![Page 37: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/37.jpg)
![Page 38: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/38.jpg)
Bring it to the Web
![Page 39: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/39.jpg)
• 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](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/40.jpg)
• 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](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/41.jpg)
• Let‘s create a controller
application/controllers/GinController.php
![Page 42: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/42.jpg)
![Page 43: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/43.jpg)
https://…/icingaweb2/showcase/gin/flavors
![Page 44: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/44.jpg)
![Page 45: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/45.jpg)
• 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](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/46.jpg)
![Page 47: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/47.jpg)
• Automatic borders• Scrollable “content”• Fixed “controls” for header
elements
![Page 48: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/48.jpg)
![Page 49: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/49.jpg)
Tab Navigation
![Page 50: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/50.jpg)
![Page 51: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/51.jpg)
![Page 52: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/52.jpg)
Menu Entries
![Page 53: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/53.jpg)
• 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](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/54.jpg)
![Page 55: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/55.jpg)
![Page 56: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/56.jpg)
Dashboards
![Page 57: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/57.jpg)
• Create own dashboard• Or extend existing ones• configuration.php
![Page 58: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/58.jpg)
![Page 59: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/59.jpg)
• Hide “controls” in dashboards
![Page 60: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/60.jpg)
![Page 61: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/61.jpg)
• 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](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/62.jpg)
![Page 63: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/63.jpg)
Styles
![Page 64: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/64.jpg)
• 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](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/65.jpg)
![Page 66: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/66.jpg)
![Page 67: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/67.jpg)
![Page 68: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/68.jpg)
Feed the View with Data
![Page 69: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/69.jpg)
• 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](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/70.jpg)
![Page 71: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/71.jpg)
![Page 72: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/72.jpg)
Translation
![Page 73: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/73.jpg)
• 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](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/74.jpg)
Configuration
![Page 75: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/75.jpg)
• /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](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/76.jpg)
[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](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/77.jpg)
Vendor CSS and JS Files
![Page 78: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/78.jpg)
![Page 79: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/79.jpg)
![Page 80: Icinga Camp Berlin 2017 - Icinga Web 2 - How to Write Modules](https://reader036.vdocument.in/reader036/viewer/2022081503/58ce74b51a28abdc578b6397/html5/thumbnails/80.jpg)
www.icinga.orgdev.icinga.orggit.icinga.org
Thank You!@icinga/icinga+icinga