InfotelGLPI / printercounters

Plugin Printercounters for GLPI
http://blogglpi.infotel.com
GNU General Public License v2.0
14 stars 9 forks source link

Add Counter view column #84

Open Yoken0S opened 3 years ago

Yoken0S commented 3 years ago

Hello,

First, thanks for this plugin.

I'd like to add the Counter of printer in a column of the global view.

I already check a little the code and make this : $tab[6090]['table'] = 'glpi_plugin_printercounters_counters'; $tab[6090]['field'] = 'value'; $tab[6090]['name'] = __('Printercounters', 'printercounters').' - '.__('Counter', 'printercounters'); $tab[6090]['datatype'] = 'number'; $tab[6090]['massiveaction'] = false; $tab[6090]['joinparams'] = ['jointype' => 'child', 'condition' => "AND NEWTABLE.value= (SELECTglpi_plugin_printercounters_counters.valueFROMglpi_plugin_printercounters_counters INNER JOINglpi_plugin_printercounters_recordsONglpi_plugin_printercounters_counters.plugin_printercounters_records_id=glpi_plugin_printercounters_records.id INNER JOINglpi_plugin_printercounters_items_recordmodelsONglpi_plugin_printercounters_records.plugin_printercounters_items_recordmodels_id =glpi_plugin_printercounters_items_recordmodels.id WHEREglpi_plugin_printercounters_items_recordmodels.items_id= REFTABLE.id) ", 'beforejoin' => ['table' => 'glpi_plugin_printercounters_items_recordmodels', 'joinparams' => ['jointype' => 'itemtype_item']] ];

But I don't really understand how works the joinparams, so I someone could help please.

eduardomozart commented 3 years ago

Hello @Yoken0S, If you have FusionInventory into your environment, you can create a SNMP task into Fusioninventory for GLPI and it will update the Counter field available into the first tab of printer devices. This field can be selected to be shown into global view.

Yoken0S commented 3 years ago

Hello @eduardomozart , Your solution seems perfect but I admit that I'm a newbie with fusioninventory I don't find where is counter in the asset of update task. For now I do a manual sql request to directly import counters from plugin counters table to printers lastcounter table.

Could help me a little more please ?

eduardomozart commented 3 years ago

Using FusionInventory for this task is a workaround and would be cool if Printercounter plugin updated the Current Counter of Pages based into "Number of printed papers" OID type from Printercounters plugin.

First, let's make sure that we're talking about the same thing. The SNMP task from FusionInventory update some information about the printer, like cartridge usage percentage and printer counter. The image below shows the counter information that is updated regularly by a SNMP task from FusionInventory:

glpi-printer-counters

I'm not sure what's your knowledge level about FusionInventory itself, but it's a solution that requires an agent that acts in name of FusionInventory Server (that is called "FusionInventory for GLPI" plug-in, a plugin that you must install into your GLPI environment so it can handle responses from FusionInventory Agent). We'll see later on that the FusionInventory Agent is called "Actor" when creating a SNMP task because it acts in name of FusionInventory Server to query some kind of information. The only thing that FusionInventory Server does is sending the request to an Actor (FusionInventory Agent), parse it's response and update the corresponding fields into GLPI itself. We call "FusionInventory for GLPI" plugin as FusionInventory Server because it's available into other ITSM tools along GLPI, like Uranos, but the way it works is almost the same in every ITSM platform:

NP113e9034NtSue6r-nS6um9XiI51t1JeC24eKCwWuOOp-C9F04N4z0eOTMVllz_awh8UHJVDKQvMdEBWWqOpUJxbi2Ah-m2e8YzPTPOWKrgCAkL4ieySca8WWFcf15oWe8eJqSyQSs5bGQ7v_0YT-5n4tTEMqxuIkojz07Sboqh2ljo506W-wYbzMowCjIyBeJn81nFvsHEn Information feedback chain of FusionInventory

You must install the FusionInventory Agent with the task NetInventory installed into some server of your farm to query SNMP information. I recommend you do in the server that hosts GLPI itself, so there's no risk that you create a SNMP schedule task and it fails to run because your other server is offline. I'll cover only Windows installation of FusionInventory Agent because it's have a GUI and it's easier, but please consider reading the instructions about installing FusionInventory Agent into Linux: http://fusioninventory.org/documentation/agent/installation/linux/rhel.html

  1. You can download FusionInventory Agent here. Please install it into a Windows 7/Server 2008 R2 or later version, previous Windows versions aren't active supported anymore.
  2. During the FusionInventory Agent installation, select the NetInventory task. When you click on it, the NetDiscovery task will be automagically detected. It happens because the NetInventory task is dependend into NetDiscovery task, as it uses the same codebase to discover (NetDiscover)/query (NetInventory) information: SNMP. windows-installer-2 3 x-screen-005-choose-components
  3. During the installation, you'll need to inform the SSL certificate from your GLPI server if you're hosting it through HTTPS and set "Only SSL to agent" into "Administration" > "FusionInventory" > "General" > "General configuration". Hopes you do it only if you know to manage a PKI infrastructure (certificates). If it's set to Yes, you changed the default config so you probably know what to do and how to export the GLPI server certificate. If you have no idea what I'm talking about, please make sure that "Only SSL to agent" is set to "No". It's a good idea study about it and limit the agent communication through a secure channel (SSL) only, so all communication between FusionInventory Agent and FusionInventory Server is encrypted. I wrote about it into my blog (Portuguese only, but Google translator is your friend): https://eduardomozartdeoliveira.wordpress.com/2020/09/02/configurando-apache-httpd-pure-ftpd-e-ispconfig-com-tls-ssl-usando-certificados-do-microsoft-ad-cs/
  4. Inform the path to your FusionInventory GLPI server URL here, pointing to the "plugins/fusioninventory" directory. It may very depending if you deploy GLPI as a subdomain (e.g. http://glpi.example.com/plugins/fusioninventory/), into the root of your webserver (e.g.: http://example.com/plugins/fusioninventory/) or into a subdirectory of your webserver (e.g.: http://example.com/glpi/plugins/fusioninventory/). You can also use the IP address instead of DNS name, but it will make it harder for you if someday you deploy some GLPI replica or if you change the IP address of your GLPI server, so I don't recommend doing it. windows-installer-2 3 x-screen-007-choose-targets
  5. I had some issues into the past because even FusionInventory Agent documentations saying that the FusionInventory Server is automatically added into the "Trusted IPs" list, but it wasn't happening to me and FusionInventory Agent was treating FusionInventory Server as an unsafe host, so it wasn't sending it's inventory information to the FusionInventory Server. I had to add the IP address of my GLPI webserver manually into the "Trusted IPs" fields to get it working, so I recommend you do the same. Also check the "Add exception on the Windows Firewall", otherwise tasks sent from FusionInventory Server through ”push” mode (server send the task to the agent, instead of waiting agent to query the pending tasks into the next scheduled inventory) may not be received by the agent. windows-installer-2 3 x-screen-011-http-server-options
  6. Select "Launch an inventory immediately after of installation" so the FusionInventory Agent will run an inventory task immediatelly after the installation. If you do not check this box, it may take about 24/2 hours to the agent being shown into FusionInventory Server (by default, the agent queries the FusionInventory Server into a time range between 12-24 hours since the last check, e.g. if the last inventory was 00:00, the next one will be executed between a time range of 12:00-23:59 - it has this mechanism to avoid a overhead of GLPI server into environments that has multiple FusionInventory Agents installed, so it avoids that all FusionInventory Agent send inventory information all at once to the GLPI server). You can also force an inventory later running the following command into the Command prompt as an administrator: "%ProgramFiles%\FusionInventory-Agent\fusioninventory-agent.bat --force --debug --debug windows-installer-2 3 x-screen-013-miscelaneous-options
  7. I recommend you enable the Debug mode by setting it to "2". It will create a fusioninventory-agent.log file into the "logs" folder of default local installation of FusionInventory Agent (%ProgramFiles%\FusionInventory-Agent). This file doesn't get that big because it has a file size limit of 16MB, where it erases itself and start from scratch. If you know how to enable Debug mode manually through the agent registry settings when needed (HKLM\Software\FusionInventory), you can keep this option to "0" and change it to "2" only when needed, if you have performance constraints about a process writing continually to a log into your server - it's not a big performance hit nowadays, however, so I recommend you keep it already enabled so you don't have to run the agent a second time just to debug an issue - the log already will record it into the first time. windows-installer-2 3 x-screen-015-debug-options

So, that's it. You can read about the installation options of FusionInventory-Agent itself into FusionInventory Documentation, if you have questions about some configuration parameters: http://fusioninventory.org/documentation/agent/installation/windows/windows-installer-2.3.x-command-line.html

The next steps requires that you understand how the tasks works into FusionInventory Server and it's communication with the agent. I created a small documentation when I started using FusionInventory because the official documentation goes direct to the point showing how to configure it but I'm the kind of person that needs to understand how every option works and how the agent/server communication happens. So I wrote the following document in attachment using DokuWiki (I recommend you adopt a kind of documentation tool too - you can use the Knowledge Base feature from GLPI, but I recommend using an external one because GLPI KB is pretty limited compared to DokuWiki, but if GLPI KB attends your needs, that's what matters).

I'm attaching my documentation in Portuguese (Brazil) and an automatic translation of it in English on how FusionInventory Server tasks works and how to create a NetInventory task. If you're Spanish or have more familiarity with Portuguese language, I recommend you read the original Portuguese (Brazil) document instead of the automatic translation, but it's better than nothing. There's the references into the document itself so if you're using the automatic translation you can click into the link available as reference to read it's original referenced content in English or French languages.

fusioninventory_-_netinventory_task en.docx

fusioninventory_-_netinventory_task.docx

If it helps, here's the configuration of my SNMP NetInventory task:

screenshot

I installed FusionInventory Agent into my own GLPI server so it uses this as the actor of action and it automatically updates the printer counter of the printer every business day between 08:00-08:30 AM. If I remove the "08:00-08:30" time interval, it will run the task and update the printer information via SNMP every 60 minutes because there's the "Interval to wake up the agent (in minutes)" restraint it to 60 minutes. If I did remove this restraint, the FusionInventory for GLPI will send the task job to the FusionInventory Agent into push mode everytime the "wakeupAgents" automatic task runs (by default, it runs every minute). I recommend you set time restraints to your task to avoid unecessary network traffic and printer overhead. More information about scheduling FusionInventory Server tasks can be found into the document attached above.

Yoken0S commented 3 years ago

Hi ! Sorry for the delay, I installed the agent on the GLPI server like you but I have some issues to make it functionnal When I created a task I have no Agent available for Actors and when I go to the status page : " The current status is waiting" "Next server target execution planned for:" Seems that agent won't work, also I used IP Ranges and not Printers because there is too many.

eduardomozart commented 3 years ago

I recommend you read the following documents that I already attached into my last comment to understand how FusionInventory tasks work before trying to configure it:

fusioninventory_-_netinventory_task en.docx

fusioninventory_-_netinventory_task.docx

By default, the "NetInventory" task isn't enabled into the agent configuration at FusionInventory Server. It's the first step of configuring a NetInventory task, as stated into the documents above.

You need to enable NetInventory into Administration > FusionInventory > General > Agents management. This page lists all FusionInventory Agents that successfully connected to your FusionInventory Server. If your FusionInventory Agent isn't showing, please review your FusionInventory Agent configuration into the comment above.

If your agent is showing up, click on it and go to "Agent modules" tab. Make sure that "NetInventory" task is enabled. Your FusionInventory Agent should be shown into "Actors" drop down right now.

Probably the "Status" tab isn't being updated because no Actors is attributed to the task itself. You'll also need to enable the refresh rate to Status page update itself if you want to follow the task progress through this tab. This option is available into the top of the "Status" tab as "Intervalo de refresh" into the screenshot above. I recommend setting it to 5 seconds.

I recommend you do not set time constraints before certificates that your task is running successfully. If you're running tasks into "Push mode", you'll also need to make sure that "wakeupAgents" automatic action is enabled. If it's set to run into "CLI" mode, it will run according to the time you setup into your cron job.

Please also make sure that the checkboxes "Active" and "Re-prepare a target-actor if previous run is successful" is enabled into your task into "Task management" tab. Click into Save and then, ONLY THEN, click into the "Force start" button to start the task immediately (otherwise it will not start the task because it's not "active", you need to save the "Active" checkbox before starting the task), but notice that the task will not run immediately if you set time constaints and the current time doesn't match the time interval of your task setup - I already opened an issue about it into https://github.com/fusioninventory/fusioninventory-for-glpi/issues/3112 -, so I do not recommend you setting up time constaints before making sure that your task is running as intended.

If you're not using the "Push" mode (My documentation about how "Push" mode works is available into the document above), after clicking into "Force start" button of the "Task management" tab, you can also force your FusionInventory Agent communicates with your FusionInventory Server through the following command:

cd /d "%ProgramFiles%\FusionInventory-Agent"
fusioninventory-agent --force --debug --debug

If you're not using Windows into your FusionInventory Agent machine, just ignore the first "cd" command and runs only the fusioninventory-agent command.

This way, you're running your FusionInventory Agent into "pull" mode, instead of "push" mode. Into "pull mode", the agent updates it's own inventory and, after doing it, it asks the FusionInventory Server if it's any available task for it to run. By default, FusionInventory Agent runs into both modes, "pull" and "push" mode, but you can limit it through your FusionInventory Agent configuration, but I recommend you keep the default behavior, so you can send tasks to execute immediately through FusionInventory Server (pull mode) instead of waiting the next inventory of your FusionInventory Agent machine (push mode).

Please notice that the "NetInventory" task requires you install additional packages into your Linux machine for it to run. It's available into Windows without additional packages, just check the "NetInventory" task during FusionInventory Agent installation. Please also make sure that your FusionInventory for GLPI plugin is updated.

Yoken0S commented 3 years ago

Hi @eduardomozart,

Thanks for your answer.

I do have the Agent in Actor as I have every Agent module enable (Excepted WoL and VMware host). But I got no item when I select Agent here some pictures to understand more : devfusion1 devfusion2 devfusion3 devfusion4 devfusion5

eduardomozart commented 3 years ago

Hello @Yoken0S,

From the screenshots, I believe that your FusionInventory Agent isn't showing into the task properties because you're changing the default behavior from FusionInventory for GLPI for newer agents.

Please check if "NetInventory" task is enabled into Administration > FusionInventory > General > Agent Management > [Agent Name] > Agent modules instead.

Please also provide the output of the following command into your FusionInventory Agent machine:

# fusioninventory-agent --list-tasks

I also updated the document above and provided screenshots to the installation and activation of NetInventory module. You can find the updated version of these documents here:

fusioninventory_-_netinventory_task en.docx

fusioninventory_-_netinventory_task.docx

It seems that your task is right. It will run every 5 minutes because you set the "Interval to wake up agents" to 5 minutes. It will fill up your database pretty quickly, because FusionInventory for GLPI creates an entry into printer History tab (Assets > Printer > [Printer name] > History tab) everytime the printer counter is updated. If you're pretending to really use this short time interval, you should consider enabling log purge into "Setup" > "General" > "Logs Purge" tab. If you set a similar time interval to the PrinterCounter plugin, I recommend you change it or execute a custom SQL query to cleanup old records from glpi_plugin_printercounters_records and glpi_plugin_printercounters_counters tables because the PrinterCounter plugin doesn't offer any mechanism to cleanup old database entries - it only allows you to remove error and empty counters records throught the "Plugin management" button available at the plugin page.

Yoken0S commented 3 years ago

Hello @eduardomozart,

Problem is I do not have Agents in Agent Management excepted those from a fusioninventory from AXEL devices Management (nothing to do with printer or snmp).

I changed the update time from 5min to 24hours (and I will increase more if it works)

Here you have the command result : commanddevglpi

Wait a minute, if I understand you glpi_plugin_printercounters_records and glpi_plugin_printercounters_counters tables are just temporary datas then implemant in glpi printers table last_count ? Because I get managed to have datas in these tables (datas still in) but never get datas transfered.

eduardomozart commented 3 years ago

If you're not seeing any agents into Administration > FusionInventory > General > Agent Configuration, it seems that your FusionInventory Agent is having trouble to communicate with your GLPI server.

Please provide a dump from HKEY_LOCAL_MACHINE\Software\FusionInventory-Agent key.

A common problem that I could find when doing a clean install of FusionInventory for GLPI is that "Service URL" does not match the GLPI server URL. Please make sure that the field "Service URL" at Administration > Entities > [Entity] > FusionInventory matches your GLPI server URL. E.g.

If you have some antivirus solution at your server and you're using SSL at your GLPI server, it maybe intercepting the connection between the FusionInventory Agent and FusionInventory Server because most of the time we use a self-signed certificate to internal services - ESET NOD32 intercepts SSL connections even if antivirus protection is disabled, you must import the *.crt certificate manually into it's "Know certificates list" (ESET Antivirus configuration > Web & Mail > SSL/TLS) to disable SSL interception between FusionInventory Agent and FusionInventory Server. It can also be possible that some Firewall maybe blocking the communication between the FusionInventory Agent and FusionInventory Server. Some antivirus solutions bundle Firewall. Please try to disable Windows Firewall and other third party Firewall solutions (if any) and try to sync the FusionInventory Agent with FusionInventory Server through the following command:

fusioninventory-agent --debug --debug --logger=stderr --force

It will print level 3 debug information of FusionInventory Agent into the command prompt window. Please paste it here to help to figure out what's going on between the communication between FusionInventory Agent and FusionInventory Server, because the agent should be showing into Administration > FusionInventory > General > Agent Configuration menu.

About PrinterCounters: you can find the database model here: https://forge.glpi-project.org/projects/glpi/wiki/SNMP_printer_counters#4Base-de-donn%C3%A9es

The value of counters is available into "value" column from "glpi_plugin_printercounters_counters" table that relates to "glpi_plugin_printercounters_records" table. The "glpi_plugin_printercounters_records" table stores information about the SNMP query done by the plugin, while the "glpi_plugin_printercounters_counters" table stores the value of the printer counter itself. If you want to update the "last_pages_counter" column from "glpi_printers" table, you need to sum the values of "values" column of "glpi_plugin_printercounters_counters" and correlates them with the "glpi_plugin_printercounters_countertypes" table (so you'll sum only OID related to the number of pages printed, like "monochromatic" and "color" prints OIDs). You'll also need to correlate the printer with it's record model. The "items_id" column from "glpi_plugin_printercounters_items_recordmodels" table correlates with the ID of the printer from "glpi_printers", so you must be able to identify which record model and it's respective OID matches every GLPI printer and update the "last_pages_counter" column from "glpi_printers" table.

Yoken0S commented 3 years ago

The Agent is on the same server that GLPI. There is no antivirus software and no firewall https connection is forced but there is no certificat and http is blocked. All connections are intranet. commanddevglpi3 commanddevglpi2

I alreay created the sql command to update from glpi_plugin_printercounters_counters I'd like a way to do it automaticly ;)

eduardomozart commented 3 years ago

It seems to be a FusionInventory Agent bug. The FusionInventory Server is denying the connection of the FusionInventory Agent. I could find a similar issue with Windows agents here: https://github.com/fusioninventory/fusioninventory-for-glpi/issues/2725 Please change the "server" value into the Windows registry to add a "/" at the end and try to force the sync again, e.g. http://glpi.example.com/plugins/fusioninventory/ Can you post the SQL query you created? There's a "Insert code" (CTRL + E) button that allows you to post code into GitHub without breaking the format. Maybe we could create an automatic action into PrinterCounter code that updates the last printer counter into GLPI, or somehow execute it after initRecord() function.

Yoken0S commented 3 years ago

You right put the / seems to work, I will answer you to confirm ;)

Of course sql request is : UPDATE glpi.glpi_printers INNER JOIN glpi_plugin_printercounters_items_recordmodels ON glpi_printers.id = glpi_plugin_printercounters_items_recordmodels.items_id INNER JOIN glpi_plugin_printercounters_records ON glpi_plugin_printercounters_records.plugin_printercounters_items_recordmodels_id = glpi_plugin_printercounters_items_recordmodels.id INNER JOIN glpi_plugin_printercounters_counters ON glpi_plugin_printercounters_counters.plugin_printercounters_records_id = glpi_plugin_printercounters_records.id SET glpi_printers.last_pages_counter= glpi_plugin_printercounters_counters.value WHERE glpi_plugin_printercounters_items_recordmodels.items_id = glpi_printers.id

eduardomozart commented 3 years ago

Your SQL code seems to work fine if a printer has only an OID counter, but if it has multiple OID counters (e.g. "monochromatic" and "color"), it does not seem to take it into account when updating the "glpi_printers.last_pages_counter" value.

Yoken0S commented 3 years ago

Ho ! I have a very few color printers here, I suppose there is another column countertype in these cases but with the same records_id ? And that why it doesn't work. If I'm right this should works.

UPDATE glpi.glpi_printers INNER JOIN glpi_plugin_printercounters_items_recordmodels ON glpi_printers.id = glpi_plugin_printercounters_items_recordmodels.items_id INNER JOIN glpi_plugin_printercounters_records ON glpi_plugin_printercounters_records.plugin_printercounters_items_recordmodels_id = glpi_plugin_printercounters_items_recordmodels.id INNER JOIN ( SELECT SUM(glpi_plugin_printercounters_counters.value) AS svalue, plugin_printercounters_records_id FROM glpi_plugin_printercounters_counters GROUP BY glpi_plugin_printercounters_counters.plugin_printercounters_records_id ) counts ON counts.plugin_printercounters_records_id = glpi_plugin_printercounters_records.id SET glpi_printers.last_pages_counter= counts.svalue WHERE glpi_plugin_printercounters_items_recordmodels.items_id = glpi_printers.id

EDITED : Updated sql request ;)

Yoken0S commented 3 years ago

Hi @eduardomozart,

There is new but still not working ^^ So I added the agent as Actor and in my printers already added there is : devglpi99 But no changes since yesterday.

I noticed that in my IP Range I didn't add the snmp credential, so I did it but still no new printers found. When I go the Network Inventory Status I got this: devglpi98

In the doubt I deleted(purge) and created the task again.

eduardomozart commented 3 years ago

Hello @Yoken0S,

Please make sure there's no time constraints applied to your task, as the screenshot below:

alt text

You need to understand how FusionInventory handle tasks before attempting to create one. As stated into the document attached below:

fusioninventory_-_netinventory_task en.docx

fusioninventory_-_netinventory_task.docx

The status "Prepared" happens when there's no time constraints (or the current time matches the time constraints) and the FusionInventory Server is waiting the agent to communicate (pull mode) and/or the next run of "wakeupAgents" task (push mode) to send the task to agent execution. Please check if the "wakeupAgents" Automatic action is enabled. I recommend you set it to "CLI" mode and configure the CRON (Task Scheduler, into your case) to execute GLPI automated tasks into background. The RDR blog posted a really nice article of how to configure it into Windows Server, if it isn't already configured: https://rdr-it.com/en/glpi-automatic-actions-configuration/

If you do not want to configure the CRON, you can also force the start of a task into "pull mode" (please read the document above that details about the differences about each mode):

It should update the task status into FusionInventory Server from "Prepared" to "Running" and the task should be executed successfully, so you can set any time contraints you want after making sure that the task is running properly.

Yoken0S commented 3 years ago

Hi @eduardomozart,

It works successfully when I do the fusioninventory-agent --force command. I will always do it with the command and do a planning task as you recommand.

Just have one question, is it possible to see if it's snmp v1 or v2c that be used ?

And did the new sql request worked by the way ?

eduardomozart commented 3 years ago

Hello @Yoken0S, The SNMP version used is determined by the SNMP credentials that you attached to the printer into "FusionInventory SNMP" tab into Printer form (Assets > Printer > [Printer Name]). You can see all SNMP credentials from FusionInventory into Administration > FusionInventory. One of the tabs has a "SNMP Credentials" name into it. I just didn't run the SQL query yet because my backup server is down so I can't restore my GLPI installation if I break it, 😂😂 But I'll run it when my backup server comes online again.

Yoken0S commented 3 years ago

@eduardomozart

Lol ok no problem ^^

For the snmp I added two credentials that why I'd like to know if it's possible to know wich one ?

eduardomozart commented 3 years ago

The SNMP credentials is a per-printer basis that you can see/change throught the "SNMP authentication" field into "FusionInventory SNMP" tab from Assets > Printer > Printer Name, like screenshot below (this printer is using SNMPv2 auth).

screenshot