[comment]: # translation:outdated

[comment]: # ({2709706a-2709706a})
# discoveryrule.create

[comment]: # ({/2709706a-2709706a})

[comment]: # ({33446799-78136d05})
### Descrizione

`object discoveryrule.create(object/array lldRules)`

Questo metodo consente di creare nuove regole LLD.

::: noteclassic
Questo metodo è disponibile solo per i tipi di utente *Admin* e *Super admin*.
Le autorizzazioni per chiamare il metodo possono essere revocate nelle impostazioni del ruolo utente.
Per ulteriori informazioni, vedere [Ruoli utente](/manual/web_interface/frontend_sections/users/user_roles).
:::

[comment]: # ({/33446799-78136d05})

[comment]: # ({fc1c8a67-1c165cc1})
### Parametri

`(object/array)` Regole LLD da creare.

Oltre alle [proprietà standard delle regole LLD](object#lld-rule), il
metodo accetta i seguenti parametri.

|Parametro|[Type](/manual/api/reference_commentary#data-types)|Descrizione|
|--|--|------|
|filter|object|[Filtro della regola LLD](/manual/api/reference/discoveryrule/object#lld-rule-filter) per la regola LLD.|
|preprocessing|object/array|Opzioni di [preprocessing della regola LLD](/manual/api/reference/discoveryrule/object#lld-rule-preprocessing).|
|lld\_macro\_paths|object/array|Opzioni [lld\_macro\_path](/manual/api/reference/discoveryrule/object#lld-macro-path) della regola LLD.|
|overrides|object/array|Opzioni di [override della regola LLD](/manual/api/reference/discoveryrule/object#lld-rule-overrides).|

[comment]: # ({/fc1c8a67-1c165cc1})

[comment]: # ({08a72633-08a72633})
### Valori restituiti

`(object)` Restituisce un oggetto contenente gli ID delle regole LLD create
sotto la proprietà `itemids`. L'ordine degli ID restituiti corrisponde all'ordine delle regole LLD approvate.

[comment]: # ({/08a72633-08a72633})

[comment]: # ({b41637d2-b41637d2})
### Esempi

[comment]: # ({/b41637d2-b41637d2})

[comment]: # ({64dbb2a9-c30fe277})
#### Creazione di una regola LLD

Creare una regola LLD di Zabbix agent per individuare i file system montati.
Gli item individuati verranno aggiornati ogni 30 secondi.

[Request](/manual/api#performing-requests):

```json
{
    "jsonrpc": "2.0",
    "method": "discoveryrule.create",
    "params": {
        "name": "Mounted filesystem discovery",
        "key_": "vfs.fs.discovery",
        "hostid": "10197",
        "type": 0,
        "interfaceid": "112",
        "delay": "30s"
    },
    "id": 1
}
```

Risposta:

```json
{
    "jsonrpc": "2.0",
    "result": {
        "itemids": [
            "27665"
        ]
    },
    "id": 1
}
```

[comment]: # ({/64dbb2a9-c30fe277})

[comment]: # ({83891df9-01b95865})
#### Utilizzo di un filtro

Creare una regola LLD con un insieme di condizioni in base alle quali filtrare i risultati.
Le condizioni verranno raggruppate utilizzando l'operatore logico "and".

[Richiesta](/manual/api#performing-requests):

```json
{
    "jsonrpc": "2.0",
    "method": "discoveryrule.create",
    "params": {
        "name": "Filtered LLD rule",
        "key_": "lld",
        "hostid": "10116",
        "type": 0,
        "interfaceid": "13",
        "delay": "30s",
        "filter": {
            "evaltype": 1,
            "conditions": [
                {
                    "macro": "{#MACRO1}",
                    "value": "@regex1"
                },
                {
                    "macro": "{#MACRO2}",
                    "value": "@regex2",
                    "operator": "9"
                },
                {
                    "macro": "{#MACRO3}",
                    "value": "",
                    "operator": "12"
                },
                {
                    "macro": "{#MACRO4}",
                    "value": "",
                    "operator": "13"
                }
            ]
        }
    },
    "id": 1
}
```

Risposta:

```json
{
    "jsonrpc": "2.0",
    "result": {
        "itemids": [
            "27665"
        ]
    },
    "id": 1
}
```

[comment]: # ({/83891df9-01b95865})

[comment]: # ({151487a3-a201a7a1})
#### Creazione di una regola LLD con percorsi macro

[Richiesta](/manual/api#performing-requests):

```json
{
    "jsonrpc": "2.0",
    "method": "discoveryrule.create",
    "params": {
        "name": "Regola LLD con percorsi macro LLD",
        "key_": "lld",
        "hostid": "10116",
        "type": 0,
        "interfaceid": "13",
        "delay": "30s",
        "lld_macro_paths": [
            {
                "lld_macro": "{#MACRO1}",
                "path": "$.path.1"
            },
            {
                "lld_macro": "{#MACRO2}",
                "path": "$.path.2"
            }
        ]
    },
    "id": 1
}
```

Risposta:

```json
{
    "jsonrpc": "2.0",
    "result": {
        "itemids": [
            "27665"
        ]
    },
    "id": 1
}
```

[comment]: # ({/151487a3-a201a7a1})

[comment]: # ({ee764cc8-32af137e})
#### Utilizzo di un filtro di espressione personalizzato

Creare una regola LLD con un filtro che utilizzi un'espressione personalizzata per
valutare le condizioni. La regola LLD deve individuare solo gli oggetti per i
quali il valore della macro "{\#MACRO1}" corrisponde a entrambe le espressioni regolari
"regex1" e "regex2", e il valore di "{\#MACRO2}" corrisponde a
"regex3" oppure "regex4". Gli ID formula "A", "B", "C" e "D" sono stati
scelti arbitrariamente.

[Richiesta](/manual/api#performing-requests):

```json
{
    "jsonrpc": "2.0",
    "method": "discoveryrule.create",
    "params": {
        "name": "Filtered LLD rule",
        "key_": "lld",
        "hostid": "10116",
        "type": 0,
        "interfaceid": "13",
        "delay": "30s",
        "filter": {
            "evaltype": 3,
            "formula": "(A and B) and (C or D)",
            "conditions": [
                {
                    "macro": "{#MACRO1}",
                    "value": "@regex1",
                    "formulaid": "A"
                },
                {
                    "macro": "{#MACRO1}",
                    "value": "@regex2",
                    "formulaid": "B"
                },
                {
                    "macro": "{#MACRO2}",
                    "value": "@regex3",
                    "formulaid": "C"
                },
                {
                    "macro": "{#MACRO2}",
                    "value": "@regex4",
                    "formulaid": "D"
                }
            ]
        }
    },
    "id": 1
}
```

Risposta:

```json
{
    "jsonrpc": "2.0",
    "result": {
        "itemids": [
            "27665"
        ]
    },
    "id": 1
}
```

[comment]: # ({/ee764cc8-32af137e})

[comment]: # ({24e10385-001d915b})
#### Utilizzo di campi di query e header personalizzati

Creare una regola LLD con campi di query e header personalizzati.

[Request](/manual/api#performing-requests):

```json
{
    "jsonrpc": "2.0",
    "method": "discoveryrule.create",
    "params": {
        "hostid": "10257",
        "interfaceid": "5",
        "type": 19,
        "name": "API HTTP agent",
        "key_": "api_discovery_rule",
        "delay": "5s",
        "url": "http://127.0.0.1?discoverer.php",
        "query_fields": [
            {
                "name": "mode",
                "value": "json"
            },
            {
                "name": "elements",
                "value": "2"
            }
        ],
        "headers": [
            {
                "name": "X-Type",
                "value": "api"
            },
            {
                "name": "Authorization",
                "value": "Bearer mF_A.B5f-2.1JcM"
            }
        ],
        "allow_traps": 1,
        "trapper_hosts": "127.0.0.1"
    },
    "id": 1
}
```

Risposta:

```json
{
    "jsonrpc": "2.0",
    "result": {
        "itemids": [
            "28336"
        ]
    },
    "id": 1
}
```

[comment]: # ({/24e10385-001d915b})

[comment]: # ({183d1530-3e76a4a9})
#### Creazione di una regola LLD con preprocessing

[Richiesta](/manual/api#performing-requests):

```json
{
    "jsonrpc": "2.0",
    "method": "discoveryrule.create",
    "params": {
        "name": "Regola di discovery con preprocessing",
        "key_": "lld.with.preprocessing",
        "hostid": "10001",
        "ruleid": "27665",
        "type": 0,
        "delay": "60s",
        "interfaceid": "1155",
        "preprocessing": [
            {
                "type": 20,
                "params": "20",
                "error_handler": 0,
                "error_handler_params": ""
            }
        ]
    },
    "id": 1
}
```

Risposta:

```json
{
    "jsonrpc": "2.0",
    "result": {
        "itemids": [
            "44211"
        ]
    },
    "id": 1
}
```

[comment]: # ({/183d1530-3e76a4a9})

[comment]: # ({1326b2e3-f9343aca})
#### Creazione di una regola LLD con override

[Richiesta](/manual/api#performing-requests):

```json
{
    "jsonrpc": "2.0",
    "method": "discoveryrule.create",
    "params": {
        "name": "Discover database host",
        "key_": "lld.with.overrides",
        "hostid": "10001",
        "type": 0,
        "delay": "60s",
        "interfaceid": "1155",
        "overrides": [
            {
                "name": "Discover MySQL host",
                "step": "1",
                "stop": "1",
                "filter": {
                    "evaltype": "2",
                    "conditions": [
                        {
                            "macro": "{#UNIT.NAME}",
                            "operator": "8",
                            "value": "^mysqld\\.service$"
                        },
                        {
                            "macro": "{#UNIT.NAME}",
                            "operator": "8",
                            "value": "^mariadb\\.service$"
                        }
                    ]
                },
                "operations": [
                    {
                        "operationobject": "3",
                        "operator": "2",
                        "value": "Database host",
                        "opstatus": {
                            "status": "0"
                        },
                        "optemplate": [
                            {
                                "templateid": "10170"
                            }
                        ],
                        "optag": [
                            {
                                "tag": "database",
                                "value": "mysql"
                            }
                        ]
                    }
                ]
            },
            {
                "name": "Discover PostgreSQL host",
                "step": "2",
                "stop": "1",
                "filter": {
                    "evaltype": "0",
                    "conditions": [
                        {
                            "macro": "{#UNIT.NAME}",
                            "operator": "8",
                            "value": "^postgresql\\.service$"
                        }
                    ]
                },
                "operations": [
                    {
                        "operationobject": "3",
                        "operator": "2",
                        "value": "Database host",
                        "opstatus": {
                            "status": "0"
                        },
                        "optemplate": [
                            {
                                "templateid": "10263"
                            }
                        ],
                        "optag": [
                            {
                                "tag": "database",
                                "value": "postgresql"
                            }
                        ]
                    }
                ]
            }
        ]
    },
    "id": 1
}
```

Risposta:

```json
{
    "jsonrpc": "2.0",
    "result": {
        "itemids": [
            "30980"
        ]
    },
    "id": 1
}
```

[comment]: # ({/1326b2e3-f9343aca})

[comment]: # ({f1471f8e-c75d7d1e})
#### Creare una regola LLD script

Creare una semplice raccolta dati utilizzando una regola LLD script.

[Request](/manual/api#performing-requests):

```json
{
    "jsonrpc": "2.0",
    "method": "discoveryrule.create",
    "params": {
        "name": "Script example",
        "key_": "custom.script.lldrule",
        "hostid": "12345",
        "type": 21,
        "params": "var request = new HttpRequest();\nreturn request.post(\"https://postman-echo.com/post\", JSON.parse(value));",
        "parameters": [{
            "name": "host",
            "value": "{HOST.CONN}"
        }],
        "timeout": "6s",
        "delay": "30s"
    },
    "id": 1
}
```

Risposta:

```json
{
    "jsonrpc": "2.0",
    "result": {
        "itemids": [
            "23865"
        ]
    },
    "id": 1
}
```

[comment]: # ({/f1471f8e-c75d7d1e})

[comment]: # ({464094bf-10111213})
#### Creare una regola LLD con un periodo di tempo specificato per la disabilitazione e senza eliminazione

Creare una regola LLD con un periodo di tempo personalizzato per disabilitare l'entità dopo
che non viene più rilevata, con l'impostazione che non verrà mai
eliminata.

[Request](/manual/api#performing-requests):

```json
{
    "jsonrpc": "2.0",
    "method": "discoveryrule.create",
    "params": {
        "name": "lld disable after 1h",
        "key_": "lld.disable",
        "hostid": "10001",
        "type": 2,
        "lifetime_type": 1,
        "enabled_lifetime_type": 0, 
        "enabled_lifetime": "1h"
    },
    "id": 1
}
```

Risposta:

```json
{
    "jsonrpc": "2.0",
    "result": {
        "itemids": [
            "46864"
        ]
    },
    "id": 1
}
```

[comment]: # ({/464094bf-10111213})

[comment]: # ({367cd00f-f40c8a6e})
### Vedi anche

-   [Filtro della regola LLD](object#lld-rule-filter)
-   [Percorsi macro LLD](object#lld-macro-path)
-   [Preelaborazione della regola LLD](object#lld-rule-preprocessing)

[comment]: # ({/367cd00f-f40c8a6e})

[comment]: # ({73c4009f-73c4009f})
### Fonte

CDiscoveryRule::create() in
*ui/include/classes/api/services/CDiscoveryRule.php*.

[comment]: # ({/73c4009f-73c4009f})
