insight in mechanics of zabbix modules · 1 get include/module.h 2 replace #include...

35
Insight in Mechanics of Zabbix Modules Gleb Ivanovsky September 9, 2016 Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 1 / 16

Upload: others

Post on 07-Jul-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

Insight in Mechanics of Zabbix Modules

Gleb Ivanovsky

September 9, 2016

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 1 / 16

Page 2: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

Outline

1 Extend ZabbixWhy?How?

2 Write modulesPrerequisitesCode exampleCompilation

3 Use modules

4 Share modules

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 2 / 16

Page 3: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

Extend Zabbix Why?

Reasons to extend Zabbix

Zabbix can’t support everything out of the box

Zabbix has certain restrictions

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 3 / 16

Page 4: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

Extend Zabbix Why?

Reasons to extend Zabbix

Zabbix can’t support everything out of the box

Zabbix has certain restrictions

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 3 / 16

Page 5: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

Extend Zabbix Why?

Reasons to extend Zabbix

Zabbix can’t support everything out of the box

Zabbix has certain restrictions

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 3 / 16

Page 6: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

Extend Zabbix How?

Ways of extending Zabbix

Scripts

script[...]

system.run[...]

UserParameter=...

Modulesserver

proxies

UNIX agents

Pros:

easy to write

easy to modify

Pros:

fast

lightweight

Cons:

slow

resource-hungry

Cons:

hard to write

modify means recompile

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 4 / 16

Page 7: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

Extend Zabbix How?

Ways of extending Zabbix

Scripts

script[...]

system.run[...]

UserParameter=...

Modulesserver

proxies

UNIX agents

Pros:

easy to write

easy to modify

Pros:

fast

lightweight

Cons:

slow

resource-hungry

Cons:

hard to write

modify means recompile

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 4 / 16

Page 8: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

Write modules Prerequisites

What you will need

Building inside Zabbix source tree

1 get Zabbix code

2 run ./bootstrap (if Zabbix code is from SVN repository)

3 run ./configure ...

Building outside of Zabbix source tree

1 get include/module.h

2 replace #include "zbxtypes.h" with

#include <int types.h>

#define zbx_uint64_t uint64_t;

3 replace #include "sysinc.h" with necessary standard headers(like #include <stdlib.h>, etc.) in module source

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 5 / 16

Page 9: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

Write modules Prerequisites

What you will need

Building inside Zabbix source tree

1 get Zabbix code

2 run ./bootstrap (if Zabbix code is from SVN repository)

3 run ./configure ...

Building outside of Zabbix source tree

1 get include/module.h

2 replace #include "zbxtypes.h" with

#include <int types.h>

#define zbx_uint64_t uint64_t;

3 replace #include "sysinc.h" with necessary standard headers(like #include <stdlib.h>, etc.) in module source

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 5 / 16

Page 10: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

Write modules Code example

Minimalistic module API

src/modules/my module/my module.c:

#include "sysinc.h" /* or standard headers here */

#include "module.h"

int zbx_module_api_version(void)

{

return ZBX_MODULE_API_VERSION;

}

include/module.h:

#define ZBX_MODULE_API_VERSION 2

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 6 / 16

Page 11: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

Write modules Code example

Implementing custom checks

include/module.h:

#define get_rkey(request) ...

#define get_rparams_num(request) ...

#define get_rparam(request , num) ...

typedef struct {...} ZBX_METRIC;

typedef struct {...} AGENT_REQUEST;

typedef struct {...} AGENT_RESULT;

#define SET_UI64_RESULT(res , val) ...

#define SET_DBL_RESULT(res , val) ...

#define SET_STR_RESULT(res , val) ...

#define SET_TEXT_RESULT(res , val) ...

#define SET_LOG_RESULT(res , val) ...

#define SET_MSG_RESULT(res , val) ...

#define SYSINFO_RET_OK 0

#define SYSINFO_RET_FAIL 1

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 7 / 16

Page 12: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

Write modules Code example

Implementing custom checks

src/modules/my module/my module.c:

static int my_hello(AGENT_REQUEST *request , AGENT_RESULT *result );

static ZBX_METRIC keys[] =

{

{"my.hello", CF_HAVEPARAMS , my_hello , "World"},

{NULL}

};

static int my_hello(AGENT_REQUEST *request , AGENT_RESULT *result)

{

char *name;

if (NULL == (name = get_rparam(request , 0)))

{

SET_MSG_RESULT(result , strdup("There is noone to greet ..."));

return SYSINFO_RET_FAIL;

}

SET_STR_RESULT(result , asprintf("Hello , %s!", name ));

return SYSINFO_RET_OK;

}

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 8 / 16

Page 13: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

Write modules Code example

Implementing history export

include/module.h:

typedef struct

{

zbx_uint64_t itemid;

int clock;

int ns;

const char *value;

const char *source;

int timestamp;

int logeventid;

int severity;

}

ZBX_HISTORY_LOG;

typedef struct

{

void (* history_float_cb )( const ZBX_HISTORY_FLOAT *history , int history_num );

void (* history_integer_cb )( const ZBX_HISTORY_INTEGER *history , int history_num );

void (* history_string_cb )( const ZBX_HISTORY_STRING *history , int history_num );

void (* history_text_cb )(const ZBX_HISTORY_TEXT *history , int history_num );

void (* history_log_cb )( const ZBX_HISTORY_LOG *history , int history_num );

}

ZBX_HISTORY_WRITE_CBS;

Supported since Zabbix 3.2

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 9 / 16

Page 14: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

Write modules Code example

Implementing history export

src/modules/my module/my module.c:

static void my_history_log_cb(const ZBX_HISTORY_LOG *history , int history_num );

ZBX_HISTORY_WRITE_CBS zbx_module_history_write_cbs(void)

{

static ZBX_HISTORY_WRITE_CBS my_callbacks =

{

NULL , /* my_history_float_cb */

NULL , /* my_history_integer_cb */

NULL , /* my_history_string_cb */

NULL , /* my_history_text_cb */

my_history_log_cb

};

return my_callbacks;

}

Supported since Zabbix 3.2

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 10 / 16

Page 15: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

Write modules Code example

Implementing history export

src/modules/my module/my module.c:

static void my_history_log_cb(const ZBX_HISTORY_LOG *history , int history_num)

{

int i;

for (i = 0; i < history_num; i++)

{

/* do something with: */

/* history[i].itemid , */

/* history[i].clock , */

/* history[i].ns , */

/* history[i].value , */

/* ... */

}

}

Supported since Zabbix 3.2

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 11 / 16

Page 16: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

Write modules Code example

Implementing anything you wish

All standard toys are at your disposal:

shared memory

semaphores

sockets

pipes

forks

files

. . .

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 12 / 16

Page 17: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

Write modules Code example

Implementing anything you wish

All standard toys are at your disposal:

shared memory

semaphores

sockets

pipes

forks

files

. . .

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 12 / 16

Page 18: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

Write modules Code example

Implementing anything you wish

All standard toys are at your disposal:

shared memory

semaphores

sockets

pipes

forks

files

. . .

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 12 / 16

Page 19: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

Write modules Code example

Implementing anything you wish

All standard toys are at your disposal:

shared memory

semaphores

sockets

pipes

forks

files

. . .

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 12 / 16

Page 20: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

Write modules Code example

Implementing anything you wish

All standard toys are at your disposal:

shared memory

semaphores

sockets

pipes

forks

files

. . .

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 12 / 16

Page 21: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

Write modules Code example

Implementing anything you wish

All standard toys are at your disposal:

shared memory

semaphores

sockets

pipes

forks

files

. . .

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 12 / 16

Page 22: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

Write modules Code example

Implementing anything you wish

All standard toys are at your disposal:

shared memory

semaphores

sockets

pipes

forks

files

. . .

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 12 / 16

Page 23: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

Write modules Code example

Implementing anything you wish

All standard toys are at your disposal:

shared memory

semaphores

sockets

pipes

forks

files

. . .

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 12 / 16

Page 24: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

Write modules Compilation

Compile module

Building inside Zabbix source tree

copy module sources to src/modules/

cd src/modules/my module

run make or gcc -fPIC -shared -o my module.so my module.c

-I../../../include

Building outside of Zabbix source tree

put module.h and my module.c into one directory

run gcc -fPIC -shared -o my module.so my module.c

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 13 / 16

Page 25: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

Write modules Compilation

Compile module

Building inside Zabbix source tree

copy module sources to src/modules/

cd src/modules/my module

run make or gcc -fPIC -shared -o my module.so my module.c

-I../../../include

Building outside of Zabbix source tree

put module.h and my module.c into one directory

run gcc -fPIC -shared -o my module.so my module.c

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 13 / 16

Page 26: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

Use modules

Start using module

Backend

1 edit configuration file:

LoadModulePath=...

LoadModule=...

LoadModule=...

...

2 restart Zabbix daemon

Frontend

1 create item2 choose Type

for server and proxy modules:Simple checkfor agent modules:Zabbix agent orZabbix agent (active)

History export is enabled automatically if loaded module exportszbx_module_history_write_cbs() function

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 14 / 16

Page 27: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

Use modules

Start using module

Backend

1 edit configuration file:

LoadModulePath=...

LoadModule=...

LoadModule=...

...

2 restart Zabbix daemon

Frontend

1 create item2 choose Type

for server and proxy modules:Simple checkfor agent modules:Zabbix agent orZabbix agent (active)

History export is enabled automatically if loaded module exportszbx_module_history_write_cbs() function

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 14 / 16

Page 28: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

Use modules

Start using module

Backend

1 edit configuration file:

LoadModulePath=...

LoadModule=...

LoadModule=...

...

2 restart Zabbix daemon

Frontend

1 create item2 choose Type

for server and proxy modules:Simple checkfor agent modules:Zabbix agent orZabbix agent (active)

History export is enabled automatically if loaded module exportszbx_module_history_write_cbs() function

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 14 / 16

Page 29: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

Share modules

Share your results

Proud of your module?

Upload it or post a link to your site athttps://share.zabbix.com/

Looking for a way to extend Zabbix?

Find a selection of brilliant modules (and other stuff) onhttps://share.zabbix.com/

Need help developing your dream module?

Zabbix Team can helphttp://www.zabbix.com/development_services.php

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 15 / 16

Page 30: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

Share modules

Share your results

Proud of your module?

Upload it or post a link to your site athttps://share.zabbix.com/

Looking for a way to extend Zabbix?

Find a selection of brilliant modules (and other stuff) onhttps://share.zabbix.com/

Need help developing your dream module?

Zabbix Team can helphttp://www.zabbix.com/development_services.php

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 15 / 16

Page 31: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

Share modules

Share your results

Proud of your module?

Upload it or post a link to your site athttps://share.zabbix.com/

Looking for a way to extend Zabbix?

Find a selection of brilliant modules (and other stuff) onhttps://share.zabbix.com/

Need help developing your dream module?

Zabbix Team can helphttp://www.zabbix.com/development_services.php

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 15 / 16

Page 32: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

Share modules

Share your results

Proud of your module?

Upload it or post a link to your site athttps://share.zabbix.com/

Looking for a way to extend Zabbix?

Find a selection of brilliant modules (and other stuff) onhttps://share.zabbix.com/

Need help developing your dream module?

Zabbix Team can helphttp://www.zabbix.com/development_services.php

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 15 / 16

Page 33: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

What are you waiting for?

Step 1:

take Zabbix

Step 2:

add few modules

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 16 / 16

Page 34: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

What are you waiting for?

Step 1:

take Zabbix

Step 2:

add few modules

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 16 / 16

Page 35: Insight in Mechanics of Zabbix Modules · 1 get include/module.h 2 replace #include "zbxtypes.h with #include #definezbx_uint64_t uint64_t; 3 replace #include "sysinc.h

What are you waiting for?

Step 1:

take Zabbix

Step 2:

add few modules

Gleb Ivanovsky (Zabbix, SIA) Insight in Mechanics of Zabbix Modules September 9, 2016 16 / 16