22

I am looking for method to check which jenkins plugins are not used. So far I found that I can look for tags in config.xml file with attribute plugin then compare them with the ones listed in plugins directory. But that does not give me complete list. Still some are not there like role-strategy.

I use python code like below

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import glob
from lxml import etree as ET
from collections import defaultdict

def find(name, path):
    return glob.glob(path+'/jobs/*/'+name)


def get_plugin_list(path):
    return [x[:-4].split('/')[-1] for x in glob.glob(path+'/plugins/*.jpi')]


if __name__ == "__main__":
    jobs_dict = defaultdict(list)
    plugins_all = set(get_plugin_list('/home/user/.jenkins')
    for config in find('config.xml', '/home/user/.jenkins'):
        with open(config) as f:
            tree = ET.XML(f.read())
            plugins = tree.xpath("/project//@plugin")
        job = config.split('/')[-2]
        for p in plugins:
            jobs_dict[p].append(job)
    with open('/home/user/.jenkins/config.xml') as f:
        tree = ET.XML(f.read())
        plugins_config = tree.xpath("/hudson//@plugin")
    plugins_used = set([x.split('@')[0] for x in jobs_dict.keys()+plugins_config])
    print "######## All plugins\n", '\n'.join(plugins_all)
    print "######## Used plugins\n", '\n'.join(plugins_used)
    print "######## Unused plugins\n", '\n'.join(plugins_all - plugins_used)
mastier
  • 872
  • 1
  • 10
  • 22

3 Answers3

34

There's a Jenkins plugin precisely for this matter: Plugin Usage

Thanks to this wonderful plugin I found many redundant plugins to remove in the Plugin Manager (you will be able to remove plugins that have no dependencies).

The plugin interface has a link on Jenkins sidebar. It lists all the plugins which have extension points in existing jobs (press on the expand button to see which jobs), and global plugins as well:

enter image description here enter image description here

Update: Pipeline jobs are now supported (Thanks Ian W)

Noam Manos
  • 15,216
  • 3
  • 86
  • 85
  • 1
    whoa! awesome, indeed nice plugin :-) many thanks for posting that :-) – mastier Mar 30 '16 at 12:30
  • 1
    @master that's great. if it answered your question, please check mark it with the V sign, thanks ;) – Noam Manos Mar 31 '16 at 14:32
  • @mastier ^ I meant – Noam Manos Sep 13 '17 at 07:29
  • when a plugin is installed, it installs few more as dependencies. This list shows the dependency plugins with zero count. So I cannot blindly delete all of them that shows zero use. How do you take care of this? – user1164061 Dec 11 '19 at 20:50
  • 7
    Plugins Usage Plugin isn't very useful IMO because it doesn't detect plugin usage from pipeline jobs. I installed this months ago and the Plugin only reports that 4/100 of my installed plugins are used and I know this to be false. Then I found their own docs admit this as well: "Plugins used in pipeline scripts would not be listed normally as used by jobs, because they are used dynamically in Jenkinsfiles." – timblaktu Mar 12 '20 at 02:32
  • @timblaktu, as of [release 4.0](https://plugins.jenkins.io/plugin-usage-plugin/releases/), it now supports pipeline analysis: "Adds analyzer for pipelines (scripted and declarative) based on last run". – Ian W Aug 26 '23 at 08:32
  • 1
    @NoamManos, no point in taking away from the top answer, but you may wish to update to indicate it supports pipelines, plus the [UI report is split into sections](https://plugins.jenkins.io/plugin-usage-plugin/) for job-specific plugin and global/system plugins; very helpful. You may want to update the image. – Ian W Aug 26 '23 at 08:36
3

Some plugins only affect the Jenkins system configuration, rather than individual jobs; you should be able to find those by changing the find method in your code to include /home/user/.jenkins/config.xml.

Many plugins have their own configuration files in $JENKINS_HOME, e.g. $JENKINS_HOME/org.jenkinsci.plugins.p4.PerforceScm.xml. I haven't looked into this, but your might be able to find some extra plugin usage by searching the config.xml files for the plugin name (e.g. PerforceSCM) rather than the term "plugin".

Also, if you only want to search for jobs that are enabled, you can filter out jobs with "<disabled>true</disabled>" in their config.xml.

gareth_bowles
  • 20,760
  • 5
  • 52
  • 82
0

If you need to check the list of installed but disabled plugins you can check Installed tab in plugin manager.

Unchecked are the ones that are disabled.

Jenkins installed plugin list

Gautam Jose
  • 686
  • 8
  • 20
  • Thanks, but it's not what I am looking for, I want list all plugins that are not used in jobs. I know that some of them are used globally, is there way to differentiate that ? That's issue. – mastier Sep 03 '15 at 15:11
  • You could try reading the job config file using the below url `http://10.157.137.17:8010/view/viewName/job/jobNm/config.xml` Replace viewname and job name with yours. Filter out the plugins using a regex. Look for `plugin=` – Gautam Jose Sep 04 '15 at 05:21
  • Thanks for an idea. I didn't know that I can access xml over http. But it's the very first solution I provided at the beginning. – mastier Sep 15 '15 at 12:12
  • Can you share the actual requirement for listing unused plugins in a job? Just curious – Gautam Jose Sep 15 '15 at 16:47
  • 1
    @GautamJose one example is when upgrading Jenkins via migration, don't want to wasted effort migrating (whether manual or automated) plugins which aren't used. Some shops unfortunately slowly but surely end up giving more and mroe people admin privs, so then you end up with a bunch of people testing plugins and abandoning those they don't find useful. – JohnZaj Oct 18 '18 at 19:29